Proper divisors: Difference between revisions
Content deleted Content added
m →version 3: re-arranged some statements for better reading. |
No edit summary |
||
Line 428: | Line 428: | ||
{num:10, divisors:{1, 2, 5}}}, |
{num:10, divisors:{1, 2, 5}}}, |
||
mostDivisors:{num:18480, divisors:79}}</lang> |
mostDivisors:{num:18480, divisors:79}}</lang> |
||
=={{header|Arc}}== |
|||
<lang Arc> |
|||
;; Given num, return num and the list of its divisors |
|||
(= divisor (fn (num) |
|||
(= dlist '()) |
|||
(when (is 1 num) (= dlist '(1 0))) |
|||
(when (is 2 num) (= dlist '(2 1))) |
|||
(unless (or (is 1 num) (is 2 num)) |
|||
(up i 1 (+ 1 (/ num 2)) |
|||
(if (is 0 (mod num i)) |
|||
(push i dlist))) |
|||
(= dlist (cons num dlist))) |
|||
dlist)) |
|||
;; Find out what number has the most divisors between 2 and 20,000. |
|||
;; Print a list of the largest known number's divisors as it is found. |
|||
(= div-lists (fn (cnt (o show 0)) |
|||
(= tlist '()) (= clist tlist) |
|||
(when (> show 0) (prn tlist)) |
|||
(up i 1 cnt |
|||
(divisor i) |
|||
(when (is 1 show) (prn dlist)) |
|||
(when (>= (len dlist) (len tlist)) |
|||
(= tlist dlist) |
|||
(when (is show 2) (prn tlist)) |
|||
(let c (- (len dlist) 1) |
|||
(push (list i c) clist)))) |
|||
(= many-divisors (list ((clist 0) 1))) |
|||
(for n 0 (is ((clist n) 1) ((clist 0) 1)) (= n (+ 1 n)) |
|||
(push ((clist n) 0) many-divisors)) |
|||
(= many-divisors (rev many-divisors)) |
|||
(prn "The number with the most divisors under " cnt |
|||
" has " (many-divisors 0) " divisors.") |
|||
(prn "It is the number " |
|||
(if (> 2 (len many-divisors)) (cut (many-divisors) 1) |
|||
(many-divisors 1)) ".") |
|||
(prn "There are " (- (len many-divisors) 1) " numbers" |
|||
" with this trait, and they are " |
|||
(map [many-divisors _] (range 1 (- (len many-divisors) 1)))) |
|||
(prn (map [divisor _] (cut many-divisors 1))) |
|||
many-divisors)) |
|||
;; Do the tasks |
|||
(div-lists 10 1) |
|||
(div-lists 20000) |
|||
;; This took about 10 minutes on my machine. |
|||
</lang> |
|||
{{Out}} |
|||
<lang Arc> |
|||
(1 0) |
|||
(2 1) |
|||
(3 1) |
|||
(4 2 1) |
|||
(5 1) |
|||
(6 3 2 1) |
|||
(7 1) |
|||
(8 4 2 1) |
|||
(9 3 1) |
|||
(10 5 2 1) |
|||
The number with the most divisors under 10 has 3 divisors. |
|||
It is the number 10. |
|||
There are 3 numbers with this trait, and they are (10 8 6) |
|||
((10 5 2 1) (8 4 2 1) (6 3 2 1)) |
|||
'(3 10 8 6) |
|||
The number with the most divisors under 20000 has 79 divisors. |
|||
It is the number 18480. |
|||
There are 2 numbers with this trait, and they are (18480 15120) |
|||
</lang> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |