;;;;; ;;;;; ;;;;; Natural Number Library ;;;;; ;;;;; (define \$nat (matcher {[,\$n [] {[\$tgt (if (eq? tgt n) {[]} {})]}] [ [] {[0 {[]}] [_ {}]}] [ nat {[\$tgt (match (compare-integer tgt 0) ordering {[ {(- tgt 1)}] [_ {}]})]}] [\$ [something] {[\$tgt {tgt}]}] })) (define \$fib (lambda [\$n] (letrec {[\$fib1 (lambda [\$n \$ret1 \$ret2] (match n nat {[ ret2] [> ret1] [ (fib1 (- n 1) (+ ret1 ret2) ret1)] }))]} (fib1 n 1 1)))) (define \$fact (lambda [\$n] (letrec {[\$fact1 (lambda [\$n \$ret] (match n [nat] {[ ret] [ (fact1 n1 (* n ret))]}))]} (fact1 n 1)))) (define \$divisor? (lambda [\$n \$d] (eq-i? 0 (remainder n d)))) (define \$gcd (lambda [\$ns] (match ns (multiset integer) {[> n] [ (gcd {m @((remove-all integer) (map (lambda [\$r] (modulo r m)) rs) 0)})]}))) ;;; ;;; Number Collections ;;; (define \$nats {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 @(map (+ 100 \$) nats)}) (define \$nats0 {0 @nats}) (define \$primes (letrec {[\$next-primes (lambda [\$primes1 \$k] (let {[\$primes2 (while (lte? \$ (floor (sqrt (itof (* 6 (+ k 1)))))) primes1)]} (match [(any (lambda [\$p] (eq? (remainder (+ (* 6 k) 1) p) 0)) primes2) (any (lambda [\$p] (eq? (remainder (+ (* 6 k) 5) p) 0)) primes2)] [bool bool] {[[,#f ,#f] {(+ (* 6 k) 1) (+ (* 6 k) 5) @(next-primes {@primes1 (+ (* 6 k) 1) (+ (* 6 k) 5)} (+ k 1))}] [[,#f ,#t] {(+ (* 6 k) 1) @(next-primes {@primes1 (+ (* 6 k) 1)} (+ k 1))}] [[,#t ,#f] {(+ (* 6 k) 5) @(next-primes {@primes1 (+ (* 6 k) 5)} (+ k 1))}] [[,#t ,#t] (next-primes primes1 (+ k 1))] })))]} {2 3 5 @(next-primes {2 3 5} 1)})) ;;; ;;; Prime Numbers ;;; (define \$find-factor (lambda [\$n] (match primes (list integer) {[> x]}))) (define \$prime-factorization (match-lambda nat {[,1 {}] [\$n (let {[\$p (find-factor n)]} {p @(prime-factorization (quotient n p))})]})) (define \$p-f prime-factorization) (define \$pfs (map (lambda [\$n] [n (p-f n)]) nats)) (define \$pfs-n (lambda [\$n] (match-all p-fs (list [integer (list integer)]) [ )] _>> [m (map (lambda [\$i] p_i) (between 1 n))]])))