Created
April 18, 2021 12:03
-
-
Save yanfeng42/f3f4103abeb9ad851b6a3d73519f68b0 to your computer and use it in GitHub Desktop.
sicp1.3 发现一个计算 Π 的方法
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 (sum term a next b) | |
(if (> a b) | |
0 | |
(+ (term a) | |
(sum term (next a) next b) | |
) | |
) | |
) | |
; 结果逼近 Π/8 | |
(define (pi-sum a b) | |
(define (pi-term x) | |
(/ 1.0 (* x (+ x 2))) | |
) | |
(define (pi-next x) | |
(+ x 4) | |
) | |
(sum pi-term a pi-next b) | |
) | |
(define (my-pi n) | |
(* 8 (pi-sum 1 n)) | |
) | |
#| | |
> (my-pi 10) | |
2.976046176046176 | |
> (my-pi 10) | |
2.976046176046176 | |
> (my-pi 100) | |
3.1215946525910105 | |
> (my-pi 1000) | |
3.139592655589783 | |
> (my-pi 10000) | |
3.141392653591793 | |
> (my-pi 100000) | |
3.141572653589795 | |
|# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment