码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-28 18:39:00      阅读:162      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!