SICP Exercise 1.31

(define (sum term a next b)
  (if (> a b)
      1
      (* (term a)
         (sum term (next a) next b))
      ))

(define (sum2 a b c from to)
  (if (= from to)
      c
      (sum2 (next b ) (next a ) (count2 (next a ) (next b ) c) (+ from 1) to))
  )

(define (next a)
     (+ a 1)
  )

(define (count2 a b c)
        (* (/ a b) c)
  )

(sum2 1 2 1 0 4)

 迭代式

 

(define (sum-131 a b )
  (sum count a inc b)
  )

(define (inc n) (+ n 1))

(define (even? n)
  (= (remainder n 2) 0)
  )

(define (count a)
    (if (even? a)
        (/ (+ a 2) (+ a 1))
        (/ (+ a 1) (+ a 2))
        ) 
  )

(sum-131 1 4)

 递归式

你可能感兴趣的:(C++,c,C#)