Aliquot sequence classifications: Difference between revisions

Add CLU
(Added 11l)
(Add CLU)
Line 843:
153557177860800: non-terminating, sequence: 153557177860800 470221741508000 685337334283120 908681172226160 1276860840159280 1867115442105104 1751034184622896 1643629718341256 1441432897905784 1647351883321016 1557892692704584 1363939602434936 1194001297910344 1597170567336056 1405725265675144 1230017019320456
</pre>
 
=={{header|CLU}}==
{{trans|C++}}
<lang clu>% This program uses the 'bigint' cluster from PCLU's 'misc.lib'
 
% Remove leading and trailing whitespace (bigint$unparse adds a lot)
strip = proc (s: string) returns (string)
ac = array[char]
sc = sequence[char]
cs: ac := string$s2ac(s)
while ~ac$empty(cs) cand ac$bottom(cs)=' ' do ac$reml(cs) end
while ~ac$empty(cs) cand ac$top(cs)=' ' do ac$remh(cs) end
% There's a bug in ac2s that makes it not return all elements
% This is a workaround
return(string$sc2s(sc$a2s(cs)))
end strip
 
divisor_sum = proc (n: bigint) returns (bigint)
own zero: bigint := bigint$i2bi(0)
own one: bigint := bigint$i2bi(1)
own two: bigint := bigint$i2bi(2)
own three: bigint := bigint$i2bi(3)
total: bigint := one
power: bigint := two
while n//two=zero do
total := total + power
power := power * two
n := n / two
end
p: bigint := three
while p*p <= n do
sum: bigint := one
power := p
while n//p = zero do
sum := sum + power
power := power * p
n := n/p
end
total := total * sum
p := p + two
end
if n>one then total := total * (n+one) end
return(total)
end divisor_sum
 
classify_aliquot_sequence = proc (n: bigint)
LIMIT = 16
abi = array[bigint]
own zero: bigint := bigint$i2bi(0)
po: stream := stream$primary_output()
terms: array[bigint] := abi$predict(0,LIMIT)
abi$addh(terms, n)
classification: string := "non-terminating"
for i: int in int$from_to(1, limit-1) do
abi$addh(terms, divisor_sum(abi$top(terms)) - abi$top(terms))
if abi$top(terms) = n then
if i=1 then classification := "perfect"
elseif i=2 then classification := "amicable"
else classification := "sociable"
end
break
end
j: int := 1
while j<i cand terms[i] ~= terms[i-j] do j := j+1 end
if j<i then
if j=1 then classification := "aspiring"
else classification := "cyclic"
end
break
end
if abi$top(terms) = zero then
classification := "terminating"
break
end
end
stream$puts(po, strip(bigint$unparse(n)) || ": " || classification || ", sequence: "
|| strip(bigint$unparse(terms[0])))
for i: int in int$from_to(1, abi$high(terms)) do
if terms[i] = terms[i-1] then break end
stream$puts(po, " " || strip(bigint$unparse(terms[i])))
end
stream$putl(po, "")
end classify_aliquot_sequence
 
start_up = proc ()
for i: int in int$from_to(1, 10) do
classify_aliquot_sequence(bigint$i2bi(i))
end
for i: int in array[int]$elements(array[int]$
[11,12,28,496,220,1184,12496,1264460,790,909,562,1064,1488]) do
classify_aliquot_sequence(bigint$i2bi(i))
end
classify_aliquot_sequence(bigint$parse("15355717786080"))
classify_aliquot_sequence(bigint$parse("153557177860800"))
end start_up</lang>
{{out}}
<pre>1: terminating, sequence: 1 0
2: terminating, sequence: 2 1 0
3: terminating, sequence: 3 1 0
4: terminating, sequence: 4 3 1 0
5: terminating, sequence: 5 1 0
6: perfect, sequence: 6
7: terminating, sequence: 7 1 0
8: terminating, sequence: 8 7 1 0
9: terminating, sequence: 9 4 3 1 0
10: terminating, sequence: 10 8 7 1 0
11: terminating, sequence: 11 1 0
12: terminating, sequence: 12 16 15 9 4 3 1 0
28: perfect, sequence: 28
496: perfect, sequence: 496
220: amicable, sequence: 220 284 220
1184: amicable, sequence: 1184 1210 1184
12496: sociable, sequence: 12496 14288 15472 14536 14264 12496
1264460: sociable, sequence: 1264460 1547860 1727636 1305184 1264460
790: aspiring, sequence: 790 650 652 496
909: aspiring, sequence: 909 417 143 25 6
562: cyclic, sequence: 562 284 220 284
1064: cyclic, sequence: 1064 1336 1184 1210 1184
1488: non-terminating, sequence: 1488 2480 3472 4464 8432 9424 10416 21328 22320 55056 95728 96720 236592 459792 881392 882384
15355717786080: non-terminating, sequence: 15355717786080 44534663601120 144940087464480 471714103310688 1130798979186912 2688948041357088 6050151708497568 13613157922639968 35513546724070632 74727605255142168 162658586225561832 353930992506879768 642678347124409032 1125102611548462968 1977286128289819992 3415126495450394808
153557177860800: non-terminating, sequence: 153557177860800 470221741508000 685337334283120 908681172226160 1276860840159280 1867115442105104 1751034184622896 1643629718341256 1441432897905784 1647351883321016 1557892692704584 1363939602434936 1194001297910344 1597170567336056 1405725265675144 1230017019320456</pre>
 
=={{header|Common Lisp}}==
2,093

edits