This commit is contained in:
NaiJi ✨ 2021-05-02 19:50:46 +03:00
parent 49dfe69e45
commit 43cb37c8ea
2 changed files with 30 additions and 0 deletions

View File

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

27
sicp-ex-1.31.rkt Normal file
View File

@ -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)