065.scm 936 B

12345678910111213141516171819202122232425262728293031323334353637
  1. ;; Convergents of e
  2. (use-modules (srfi srfi-1)
  3. (euler utils)
  4. (euler generators))
  5. (define-syntax rec
  6. (syntax-rules ()
  7. [(_ x e) (letrec ([x e]) x)]))
  8. (define (num-digits-sum-for-nth-convergent convergents frac-generator)
  9. (define (nth-convergent)
  10. (let loop ((i (1- convergents)) (curr-frac (frac-generator)))
  11. (if (zero? i) curr-frac
  12. (loop (1- i) (frac-generator)))))
  13. (fold + 0
  14. (number->digits
  15. (numerator (nth-convergent)))))
  16. (define (make-convergents-of-e-generator)
  17. (let ((frac-generator (make-convergent-frac-generator))
  18. (input-lst (list 1 2 1))
  19. (initialized? #f))
  20. (lambda ()
  21. (if initialized?
  22. (let ((term (frac-generator (car input-lst))))
  23. (set! input-lst
  24. (append (cdr input-lst)
  25. (list
  26. (if (> (car input-lst) 1)
  27. (+ 2 (car input-lst))
  28. (car input-lst)))))
  29. term)
  30. (begin
  31. (set! initialized? #t)
  32. (frac-generator 2))))))