057.scm 708 B

123456789101112131415161718192021222324252627282930
  1. ;; Square root convergents
  2. (use-modules (srfi srfi-1)
  3. (euler utils))
  4. (define (convergent-fracs-where-num-digits>denom-digits expansions)
  5. (define generate-frac (make-sqrt-convergent-generator 2))
  6. (let loop ((i expansions) (curr-frac (generate-frac)) (acc 0))
  7. (if (zero? i) acc
  8. (loop (1- i)
  9. (generate-frac)
  10. (if (digits>? (numerator curr-frac)
  11. (denominator curr-frac))
  12. (1+ acc)
  13. acc)))))
  14. (define (make-sqrt-convergent-generator n)
  15. (let ((*k* #f))
  16. (lambda ()
  17. (call-with-prompt
  18. 'expand
  19. (lambda ()
  20. (if *k*
  21. (*k* (+ n (/ 1 (abort-to-prompt 'expand))))
  22. (+ 1 (/ 1 (abort-to-prompt 'expand)))))
  23. (lambda (k)
  24. (set! *k* k)
  25. (k n))))))