Last active
March 14, 2017 12:59
-
-
Save Yoxem/168556fb00d2d93abba8c4430eea6e87 to your computer and use it in GitHub Desktop.
Find the approximation of pi
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 (pi-iter i max sum) | |
(if (= i max) | |
(* 4 sum) | |
(let ((k (/ | |
(^ (- 1) i) | |
(+ (* 2 i) 1)))) | |
(pi-iter (+ i 1) max (+ | |
sum | |
k))) | |
)) | |
(define (^ x y) | |
(cond ((= y 0) 1) | |
((= y 1) x) | |
(else (^-iter x 1 y x))) | |
) | |
(define (^-iter x i max result) | |
(if (= i max) result | |
(^-iter x (+ i 1) max (* x result)) | |
)) | |
(display "pi的近似值:") | |
(display (pi-iter 0 50000 0)) | |
(newline) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment