Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,54 @@
;; implementation of Floyd algorithm to find cycles in a graph
;; see Wikipedia https://en.wikipedia.org/wiki/Cycle_detection
;; returns (cycle-length cycle-starter steps)
;; steps = 0 if no cycle found
;; it's all about a tortoise 🐢 running at speed f(x) after a hare 🐰 at speed f(f (x))
;; when they meet, a cycle is found
(define (floyd f x0 steps maxvalue)
(define lam 1) ; cycle length
(define tortoise (f x0))
(define hare (f (f x0)))
;; cyclic ? yes if steps > 0
(while (and (!= tortoise hare) (> steps 0))
(set!-values (tortoise hare) (values (f tortoise) (f (f hare))))
#:break (and (> hare maxvalue) (set! steps 0))
(set! steps (1- steps)))
;; first repetition = cycle starter
(set! tortoise x0)
(while (and (!= tortoise hare) (> steps 0))
(set!-values (tortoise hare) (values (f tortoise) (f hare))))
;; length of shortest cycle
(set! hare (f tortoise))
(while (and (!= tortoise hare) (> steps 0))
(set! hare (f hare))
(set! lam (1+ lam)))
(values lam tortoise steps))
;; find cycle and classify
(define (taxonomy n (steps 16) (maxvalue 140737488355328))
(define-values (cycle starter steps) (floyd sum-divisors n steps maxvalue))
(write n
(cond
(( = steps 0) 'non-terminating)
(( = starter 0) 'terminating)
((and (= starter n) (= cycle 1)) 'perfect)
((and (= starter n) (= cycle 2)) 'amicable)
((= starter n) 'sociable )
((= cycle 1) 'aspiring )
(else 'cyclic)))
(aliquote n starter)
)
;; print sequence
(define (aliquote x0 (starter -1) (end -1 )(n 8))
(for ((i n))
(write x0)
(set! x0 (sum-divisors x0))
#:break (and (= x0 end) (write x0))
(when (= x0 starter) (set! end starter)))
(writeln ...))

View file

@ -0,0 +1,39 @@
(lib 'math)
(lib 'bigint)
(for-each taxonomy (range 1 13))
1 terminating 1 0 0 ...
2 terminating 2 1 0 0 ...
3 terminating 3 1 0 0 ...
4 terminating 4 3 1 0 0 ...
5 terminating 5 1 0 0 ...
6 perfect 6 6 6 ...
7 terminating 7 1 0 0 ...
8 terminating 8 7 1 0 0 ...
9 terminating 9 4 3 1 0 0 ...
10 terminating 10 8 7 1 0 0 ...
11 terminating 11 1 0 0 ...
12 terminating 12 16 15 9 4 3 1 0 0 ...
(for-each taxonomy '( 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080))
28 perfect 28 28 28 ...
496 perfect 496 496 496 ...
220 amicable 220 284 220 284 220 ...
1184 amicable 1184 1210 1184 1210 1184 ...
12496 sociable 12496 14288 15472 14536 14264 12496 14288 15472 ...
1264460 sociable 1264460 1547860 1727636 1305184 1264460 1547860 1727636 1305184 1264460 ...
790 aspiring 790 650 652 496 496 ...
909 aspiring 909 417 143 25 6 6 ...
562 cyclic 562 284 220 284 ...
1064 cyclic 1064 1336 1184 1210 1184 ...
1488 non-terminating 1488 2480 3472 4464 8432 9424 10416 21328 ...
15355717786080 non-terminating 15355717786080 44534663601120 144940087464480 471714103310688 1130798979186912 2688948041357088 6050151708497568 13613157922639968 ...
(taxonomy 1000) ;; 1000 non-terminating after 16 steps
1000 non-terminating 1000 1340 1516 1144 1376 1396 1054 674 ...
(taxonomy 1000 32) ;; but terminating if we increase the number of steps
1000 terminating
1000 1340 1516 1144 1376 1396 1054 674 340 416 466 236 184 176 196 203 37 1 0 0 ...