Skip to content

Instantly share code, notes, and snippets.

@JoeCooper
Created September 8, 2017 22:14
Show Gist options
  • Select an option

  • Save JoeCooper/ae953a1ffe48a20f29b1450ad30fef67 to your computer and use it in GitHub Desktop.

Select an option

Save JoeCooper/ae953a1ffe48a20f29b1450ad30fef67 to your computer and use it in GitHub Desktop.
Leibniz formula for π in Scheme. As iteration count approaches infinity, result approaches π.
(define (leibnizFormulaForPi steps)
(define (stage step) (* (/ 1 (+ 1 (* step 2))) (if (= (modulo step 2) 1) -1 1)))
(define (iterate thusFar step steps)
(if (< step steps)
(iterate (+ thusFar (stage step)) (+ step 1) steps)
(+ thusFar (stage step))
)
)
(* 4 (iterate 0 0 steps))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment