Browse Source

Add 1.31

master
NaiJi 6 months ago
parent
commit
43cb37c8ea
  1. 3
      sicp-ex-1.16.rkt
  2. 27
      sicp-ex-1.31.rkt

3
sicp-ex-1.16.rkt

@ -1,3 +1,6 @@
#! /usr/bin/racket
#lang racket/base
(define (sqr n)
(* n n))

27
sicp-ex-1.31.rkt

@ -0,0 +1,27 @@
#! /usr/bin/racket
#lang racket/base
(define (product term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* (term a) result))))
(iter a 1))
(define (numerator pre-applying n)
(* pre-applying (product (lambda (x) (* x x)) 4 (lambda (x) (+ x 2)) n)))
(define (denominator pre-applying n)
(* pre-applying (product (lambda (x) (* x x)) 3 (lambda (x) (+ x 2)) n)))
(define (fourth-of-pi n)
(cond ((< n 1) 0)
((even? n) (/ (numerator (* 2 (+ n 2)) n)
(denominator 1 ( + n 1))))
(else (/ (numerator 2 (+ n 1))
(denominator (+ n 2) n)))))
(fourth-of-pi 1)
(fourth-of-pi 2)
(fourth-of-pi 3)
(fourth-of-pi 60)
Loading…
Cancel
Save