首页 > 其他 > 详细

SICP:1.31按照公式求Pi值,原理同1.29

时间:2015-03-28 18:38:37      阅读:147      评论:0      收藏:0      [点我收藏+]
#lang racket
;product doing the multiplation
(define (product term a next b)
  (if (< b a)
      1
      (* (term a)
     (product term (next a) next b))
   );if
  );product

(define (product-iteration term a next b)
  (define (iteration a result)
    (if (< b a)
    result
    (iteration (next a) 
           (* result (term a));*
           );iteration
    );if
    );iteration
  (iteration a 1)
  );product-iteration


(define (get-pi n)
  (define a 1.0)
  (define (odd-num c) 
    (+ 1 (* 2 c));+
    );odd-num
  (define (even-num c)
    (* c 2));even-num
  (define (term1 c)
    (/ (even-num c)
       (odd-num c));/
    );term1
  (define (term2 c)
    (/ (+ 1 (odd-num c))
       (odd-num c));/
    );term2
  (define (next c)
    (+ c 1));next
  
  (* 4
     (product-iteration term1 a next n)
     (product-iteration term2 a next n))
  );get-pi

(get-pi 1000)
(get-pi 10000)

技术分享

SICP:1.31按照公式求Pi值,原理同1.29

原文:http://www.cnblogs.com/wizzhangquan/p/4374481.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!