Skip to content

Instantly share code, notes, and snippets.

@yanfeng42
Created April 18, 2021 12:03
Show Gist options
  • Save yanfeng42/f3f4103abeb9ad851b6a3d73519f68b0 to your computer and use it in GitHub Desktop.
Save yanfeng42/f3f4103abeb9ad851b6a3d73519f68b0 to your computer and use it in GitHub Desktop.
sicp1.3 发现一个计算 Π 的方法
(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