Skip to content

Instantly share code, notes, and snippets.

@jokester
Created May 22, 2014 02:18
Show Gist options
  • Save jokester/c05ae1dd9e4069671694 to your computer and use it in GitHub Desktop.
Save jokester/c05ae1dd9e4069671694 to your computer and use it in GitHub Desktop.
(define (inc i) (+ i 1))
(define (product1 term a next b)
(if (> a b)
1
(* (term a)
(product1 term (next a) next b))))
(define (product2 term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* (term a) result))))
(iter a 1))
(define (pi/4 n)
(define (nom k)
(cond
((= 1 k) 2)
((even? k) (+ 2 k))
(else (+ 1 k))))
(define (denom k)
(cond
((even? k) (+ 1 k))
(else (+ 2 k))))
(define (term k) (/ (nom k) (denom k)))
(product2 term 1 inc n))
(define (pi n)
(* 4.0 (pi/4 n)))
(pi 100000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment