072.scm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;; Ordered fractions (71->73)
  2. (use-modules (srfi srfi-1))
  3. (define (ordered-fractions max-denom)
  4. (let loop ((num 1) (denom 2) (acc '()))
  5. (cond
  6. ((>= denom max-denom) acc)
  7. ((>= num denom)
  8. ;(if (zero? (modulo denom 1000)) (begin (display "denom: ") (display denom) (newline)) #f)
  9. (loop 1 (1+ denom) acc))
  10. (else (loop (1+ num) denom (cons num acc))))))
  11. (define (left-of-frac frac max-denom)
  12. (let loop ((num 1) (denom 2) (curr-best 0))
  13. (cond
  14. ((>= denom max-denom) curr-best)
  15. ((>= num denom) (loop 1 (1+ denom) curr-best))
  16. (else
  17. (loop
  18. (1+ num) denom
  19. (let ((best-diff (- frac curr-best))
  20. (curr-diff (- frac (/ num denom))))
  21. (cond
  22. ((zero? curr-diff) curr-best)
  23. ((< curr-diff 0) curr-best)
  24. ((> curr-diff best-diff) curr-best)
  25. (else (/ num denom)))))))))
  26. (define (enumeration-test max-denom)
  27. (let loop ((num 1) (denom 2))
  28. (cond
  29. ((> denom max-denom) #f)
  30. ((> num denom)
  31. (if (zero? (modulo denom 10000)) (begin (display denom) (newline)) #f)
  32. (loop 1 (1+ denom)))
  33. (else (loop (1+ num) denom)))))
  34. (display (enumeration-test (expt 10 6)))
  35. (define (fractions-between-fast min-frac max-frac max-denom)
  36. (let loop ((num 1) (denom 2) (fracs-between '()))
  37. (cond
  38. ((> denom max-denom) fracs-between)
  39. ((> num denom) (loop 1 (1+ denom) fracs-between))
  40. ((let ((curr-frac (/ num denom)))
  41. (and (> curr-frac min-frac)
  42. (< curr-frac max-frac)
  43. (not (memq curr-frac fracs-between))))
  44. (display (length fracs-between)) (newline)
  45. (loop (1+ num) denom (cons (/ num denom) fracs-between)))
  46. (else (loop (1+ num) denom fracs-between)))))
  47. (define (fractions-between min-frac max-frac max-denom)
  48. (define ordered-fracs (delete-duplicates-sorted
  49. (sort (ordered-fractions max-denom) <)))
  50. (let loop ((curr-lst ordered-fractions) (fracs-between '()))
  51. (if (null? curr-lst) fracs-between
  52. (let ((curr-frac (car ordered-fractions)))
  53. (cond
  54. ((> curr-frac max-frac) fracs-between)
  55. ((and (> curr-frac min-frac) (< curr-frac max-frac))
  56. (loop (cdr ordered-fractions) (cons curr-frac fracs-between)))
  57. (else (loop (cdr ordered-fractions) fracs-between)))))))
  58. (define (number-of-generated-fractions max-denom)
  59. (define fraction-dict '())
  60. 0)
  61. (define (delete-duplicates-sorted lst)
  62. (let loop ((curr-lst lst) (unique-lst '()))
  63. (cond
  64. ((null? curr-lst) unique-lst)
  65. ((null? (cdr curr-lst)) (cons (car curr-lst) unique-lst))
  66. (else (loop (cdr curr-lst)
  67. (if (= (car curr-lst) (cadr curr-lst))
  68. unique-lst
  69. (cons (car (curr-lst)) unique-lst)))))))
  70. ;; (display (left-of-frac (/ 3 7) (expt 10 4)))
  71. ;;(display (left-of-frac (/ 3 7) (expt 10 6)))
  72. ;;(display (fractions-between-fast (/ 1 3001) (/ 1 3000) 12000))