1. ; fibonacho numbers
2.
3. (define (range . args)
4. (case (length args)
5. ((1) (range 0 (car args) (if (negative? (car args)) -1 1)))
6. ((2) (range (car args) (cadr args) (if (< (car args) (cadr args)) 1 -1)))
7. ((3) (let ((le? (if (negative? (caddr args)) >= <=)))
8. (let loop ((x(car args)) (xs '()))
9. (if (le? (cadr args) x)
10. (reverse xs)
11. (loop (+ x (caddr args)) (cons x xs))))))
12. (else (error 'range "unrecognized arguments"))))
13.
14. (define (iterate n f . bs)
15. (let loop ((n n) (b (car bs)) (bs (cdr bs)) (xs '()))
16. (if (zero? n) (reverse xs)
17. (let ((new-bs (append bs (list (apply f b bs)))))
18. (loop (- n 1) (car new-bs) (cdr new-bs) (cons b xs))))))
19.
20. (define fibs (iterate 20 + 1 1))
21.
22. (define (scan-left op base xs)
23. (define (scan base xs)
24. (if (null? xs) (list)
25. (scan-left op (op base (car xs)) (cdr xs))))
26. (cons base (scan base xs)))
27.
28. (define sums (cdr (scan-left + 0 fibs)))
29.
30. (define (restarts n)
31. (define (max-less-or-equal n xs)
32. (let loop ((prev 0) (xs xs))
33. (if (< n (car xs)) prev
34. (loop (car xs) (cdr xs)))))
35. (let loop ((n n) (r 0))
36. (if (zero? n) r
37. (loop (- n (max-less-or-equal n sums)) (+ r 1)))))
38.
39. ; https://p...content-available-to-author-only...s.com/2016/12/20/highly-abundant-numbers
40. (define (records lt? xs) ; index and value at each new maximum
41. (if (null? xs) (error 'records "no data")
42. (let loop ((xs (cdr xs)) (k 1) (recs (list (cons 0 (car xs)))))
43. (if (null? xs) (reverse recs)
44. (if (lt? (cdar recs) (car xs))
45. (loop (cdr xs) (+ k 1) (cons (cons k (car xs)) recs))
46. (loop (cdr xs) (+ k 1) recs))))))
47.
48. (define (fib n)
49. (define (square x) (* x x))
50. (cond ((zero? n) 0) ((or (= n 1) (= n 2)) 1)
51. ((even? n) (let* ((n2 (quotient n 2)) (n2-1 (- n2 1)))
52. (* (fib n2) (+ (* 2 (fib n2-1)) (fib n2)))))
53. (else (let* ((n2-1 (quotient n 2)) (n2 (+ n2-1 1)))
54. (+ (square (fib n2-1)) (square (fib n2)))))))
55.
56. (define (fibonacho n) (- (fib (+ n n 1)) n))
57.
58. (display fibs) (newline)
59. (display sums) (newline)
60. (display (restarts 227)) (newline)
61.
62. (for-each
63. (lambda (idx/val)
64. (display (cdr idx/val)) (display " ")
65. (display (+ (car idx/val) 1)) (newline))
66. (records < (map restarts (range 1 1000))))
67.
68. (display (map fibonacho (range 1 50))) (newline)
Success #stdin #stdout 0.27s 9216KB
stdin
Standard input is empty
stdout
(1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765)
(1 2 4 7 12 20 33 54 88 143 232 376 609 986 1596 2583 4180 6764 10945 17710)
6
1 1
2 3
3 10
4 30
5 84
6 227
7 603
(1 3 10 30 84 227 603 1589 4172 10936 28646 75013 196405 514215 1346254 3524562 9227448 24157799 63245967 165580121 433494416 1134903148 2971215050 7778742025 20365011049 53316291147 139583862418 365435296134 956722026012 2504730781931 6557470319811 17167680177533 44945570212820 117669030460960 308061521170094 806515533049357 2111485077978013 5527939700884719 14472334024676182 37889062373143866 99194853094755456 259695496911122543 679891637638612215 1779979416004714145 4660046610375530264 12200160415121876692 31940434634990099858 83621143489848422929 218922995834555168977)