Created
April 25, 2021 02:47
-
-
Save yanfeng42/ff5343239352ae5942fd64b574082837 to your computer and use it in GitHub Desktop.
sicp 1.31 另一个关于 pi 的计算公式 -- john wallis 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 (product term a next b) | |
(if (> a b) | |
1 | |
(* (term a) | |
(product term (next a) next b) | |
) | |
) | |
) | |
(define (product term a next b) | |
(define (iter a result) | |
(if (> a b) | |
result | |
(iter (next a) (* (term a) result)) | |
) | |
) | |
(iter a 0) | |
) | |
; 阶乘函数: n! = n * (n - 1) * (n - 2) * ... 3 * 2 * 1 | |
(define (factorial n) | |
(define (term x) x) | |
(define (next x) (+ 1 x)) | |
(product term 1 next n) | |
) | |
; 规律: (1 - 1/3) * (1 + 1/3) * (1 - 1/5) * (1 + 1/5) | |
; item: 2N+1 | |
(define (wallis-pi n) | |
(define (wallis) | |
(define (term x) | |
(define i (/ 1.0 (+ (* 2 x) 1))) | |
(* (- 1 i) (+ 1 i)) | |
) | |
(define (next x) | |
(+ x 1) | |
) | |
(product term 1 next n) | |
) | |
(* 4 (wallis)) | |
) | |
#| | |
> (wallis-pi 100) | |
3.1493784731685985 | |
> (wallis-pi 1000) | |
3.1423773650938758 | |
> (wallis-pi 10000) | |
3.1416711865344302 | |
> (wallis-pi 100000) | |
3.141600507501392 | |
> (wallis-pi 1000000) | |
3.141593438980562 | |
> (wallis-pi 10000000) | |
3.1415927321150257 | |
|# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ref: http://community.schemewiki.org/?sicp-ex-1.31
ref: https://en.wikipedia.org/wiki/Wallis_product
和社区答案, 对于公式的形式化理解有差异.
我的第一反应是拆成: (1 - 1/3) * (1 + 1/3) * (1 - 1/5) * (1 + 1/5) * ...