065.scm 862 B

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