Created
May 22, 2014 02:18
-
-
Save jokester/c05ae1dd9e4069671694 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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