Last active
June 6, 2017 13:20
-
-
Save leiming/1afd1ea80b6f492e7b3cec184357d08c to your computer and use it in GitHub Desktop.
ex01_1.38.scm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang sicp | |
(define (cont-d i) | |
(let ((n (floor (/ (+ i 1) 3)))) | |
(if (= 0 (remainder (+ i 1) 3)) | |
(* 2 n) | |
1))) | |
(define (cont-frac n d k) | |
(define (iter i result) | |
(if (< i 1) | |
result | |
(iter (- i 1) | |
(/ (n i) | |
(+ (d i) result))))) | |
(iter k 1)) | |
(define (cont-frac-iter n d k) | |
(define (iter i result) | |
(if (= k i) | |
result | |
(iter (+ i 1) | |
(/ (n i) | |
(+ (d i) result))))) | |
(iter 1 1)) | |
(cont-frac (lambda (i) 1.0) | |
(lambda (i) 1.0) | |
30000) | |
(cont-frac-iter (lambda (i) 1.0) | |
(lambda (i) 1.0) | |
30000) | |
(cont-frac (lambda (i) 1.0) | |
cont-d | |
30000) | |
(cont-frac-iter (lambda (i) 1.0) | |
cont-d | |
30000) | |
;;; result | |
; 0.6180339887498948 | |
; 0.6180339887498948 | |
; 0.7182818284590453 | |
; 4.999874999999922e-05 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment