Skip to main content
  1. Blog/

Pi Day

programming holiday Processing Lisp pi
Phil Chu
Author
Phil Chu
Making software since the 80s

Happy Pi Day! This day brings back to my high school days during which I found a library book explaining ways to calculate pi and programmed a couple of them on an Apple II.

Decades later, I tried that again in Processing (code is on GitHub), which looks nice visually:

But I like how the code looks in Gauche Scheme (code also on GitHub)

(define (pi radius)
 (let f ((inside 0)
         (total 0))
  (print (* 4.0 (/ inside (+ 0.000000001 total))))
  (if (incircle? (random radius) (random radius) radius)
   (f (+ 1 inside) (+ 1 total))
   (f inside (+ 1 total)))))

(define (random range)
 (- (* (random-real) (+ range range)) range))

(define (incircle? x y radius)
 (< (+ (square x) (square y)) (square radius)))