Skip to content

Instantly share code, notes, and snippets.

@jself
Created October 15, 2014 14:47
Show Gist options
  • Save jself/9f542340ea44baa672d0 to your computer and use it in GitHub Desktop.
Save jself/9f542340ea44baa672d0 to your computer and use it in GitHub Desktop.
;load stuffs -- not sure why buildapp doesn't do this
#-quicklisp
(let ((quicklisp-init (merge-pathnames "quicklisp/setup.lisp"
(user-homedir-pathname))))
(when (probe-file quicklisp-init)
(load quicklisp-init)))
(ql:quickload :lparallel)
(use-package :lparallel)
;vars
(defvar *n* 4)
(defvar *iterations* 100000000)
;settings for compiler/random
(declaim (optimize (speed 3) (safety 0) (debug 0) (compilation-speed 0)))
(setf *random-state* (make-random-state t))
;the code
(define-compiler-macro in-circle (x y)
(declare (type single-float x))
(declare (type single-float y))
`(<= (sqrt (+ (* ,x ,x) (* ,y ,y))) 1.0))
(defun looppi (attempts)
(let ((hits 0))
(dotimes (i attempts)
(let ((x (random 1.0))
(y (random 1.0)))
(declare (type single-float x))
(declare (type single-float y))
(if (in-circle x y)
(incf hits)
)))
hits))
(defun main (args)
(setf lparallel:*kernel* (lparallel:make-kernel *n*))
(let ((channel (make-channel))
(hits 0))
(declare (type integer hits))
(loop for i from 1 to *n* do
(submit-task channel 'looppi (/ *iterations* *n*)))
(submit-task channel 'looppi (mod *iterations* *n*))
(loop for i from 1 to (+ *n* 1) do
(setf hits (+ hits (receive-result channel))))
(format t "Hits: ~a~%" hits)
(format t "Pi ~a~%" (* 4 (/ (float hits) *iterations*)))))
(setf *random-state* (make-random-state t))
(defun in-circle (x y)
(declare (optimize (speed 3) (safety 0)))
(declare (type single-float x))
(declare (type single-float y))
(<= (sqrt (float (+ (* x x) (* y y)))) 1.0))
(defun looppi (attempts)
(declare (optimize (speed 3) (safety 0)))
(let ((hits 0)
(misses 0))
(dotimes (i attempts)
(let ((x (random 1.0))
(y (random 1.0)))
(if (in-circle x y)
(incf hits)
(incf misses))))
(float (* 4.0 (/ (float hits) (+ hits misses))))))
;(defun main (args)
(defun main (args)
(format t "Pi: ~a~%" (looppi 100000000)))
package main
import "fmt"
import "math/rand"
import "math"
import "time"
func incircle (x float64, y float64) bool {
x = math.Pow(x, 2)
y = math.Pow(y, 2)
val := math.Sqrt(x + y)
return val <= 1.0
}
func main() {
source := rand.NewSource(time.Now().Unix())
r := rand.New(source)
var hits float64 = 0.0
var misses float64 = 0.0
for i := 1; i <= 1000000; i++ {
if (incircle(r.Float64(), r.Float64())) {
hits += 1
} else {
misses += 1
}
}
fmt.Print("Pi: ")
fmt.Print(4*(hits/(hits + misses)))
fmt.Println()
}
#adapted from http://niallohiggins.com/2007/07/05/monte-carlo-simulation-in-python-1/
from random import random
from math import pow, sqrt
iterations = 100000000
def pi():
hits = 0
for i in xrange (1, iterations):
x = random()
y = random()
dist = sqrt(x*x + y*y)
if dist <= 1.0:
hits = hits + 1.0
# hits / throws = 1/4 Pi
pi = 4 * (hits / iterations)
print "pi = %s" %(pi)
pi()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment