The Way 2 Inner Peace.

得到Y组合子

欧拉项目第65题.

Qians posted @ 2014年12月12日 22:22 in scheme with tags Scheme , 1698 阅读
#lang racket

(define (k-con n f init)
  (let loop([i n] [r 0])
    (cond [(= 1 i) (+ init r)]
          [else (loop (sub1 i) (/ 1 (+ r (f i))))])))
          
;; ith convergent of sqrt(2)
(define (sqrt2 i)
  (k-con i (λ (x) 2) 1))

;; ith convergent of e
(define (con-e i)
  (k-con i (λ (x) 
             (let-values ([(q r) (quotient/remainder x 3)])
               (if (zero? r) (* 2 q) 1)))
         2))

(numerator (con-e 100))

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter