Aliquot sequence classifications: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: adjusted for correct case for sequence type.) |
(→{{header|Julia}}: A new entry for Julia) |
||
Line 303: | Line 303: | ||
15355717786080: non-terminating: [15355717786080,44534663601120]</lang> |
15355717786080: non-terminating: [15355717786080,44534663601120]</lang> |
||
=={{header|Julia}}== |
|||
'''Core Function''' |
|||
<lang Julia> |
|||
function aliquotclassifier{T<:Integer}(n::T) |
|||
a = T[n] |
|||
b = divisorsum(a[end]) |
|||
len = 1 |
|||
while len < 17 && !(b in a) && 0 < b && b < 2^47+1 |
|||
push!(a, b) |
|||
b = divisorsum(a[end]) |
|||
len += 1 |
|||
end |
|||
if b in a |
|||
1 < len || return ("Perfect", a) |
|||
if b == a[1] |
|||
2 < len || return ("Amicable", a) |
|||
return ("Sociable", a) |
|||
elseif b == a[end] |
|||
return ("Aspiring", a) |
|||
else |
|||
return ("Cyclic", push!(a, b)) |
|||
end |
|||
end |
|||
push!(a, b) |
|||
b != 0 || return ("Terminating", a) |
|||
return ("Non-terminating", a) |
|||
end |
|||
</lang> |
|||
'''Supporting Functions''' |
|||
<lang Julia> |
|||
function pcontrib{T<:Integer}(p::T, a::T) |
|||
n = one(T) |
|||
pcon = one(T) |
|||
for i in 1:a |
|||
n *= p |
|||
pcon += n |
|||
end |
|||
return pcon |
|||
end |
|||
function divisorsum{T<:Integer}(n::T) |
|||
dsum = one(T) |
|||
for (p, a) in factor(n) |
|||
dsum *= pcontrib(p, a) |
|||
end |
|||
dsum -= n |
|||
end |
|||
</lang> |
|||
'''Main''' |
|||
<lang Julia> |
|||
println("Classification Tests:") |
|||
tests = [1:12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488] |
|||
for i in tests |
|||
(class, a) = aliquotclassifier(i) |
|||
println(@sprintf("%8d => ", i), @sprintf("%16s, ", class), a) |
|||
end |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Classification Tests: |
|||
1 => Terminating, [1,0] |
|||
2 => Terminating, [2,1,0] |
|||
3 => Terminating, [3,1,0] |
|||
4 => Terminating, [4,3,1,0] |
|||
5 => Terminating, [5,1,0] |
|||
6 => Perfect, [6] |
|||
7 => Terminating, [7,1,0] |
|||
8 => Terminating, [8,7,1,0] |
|||
9 => Terminating, [9,4,3,1,0] |
|||
10 => Terminating, [10,8,7,1,0] |
|||
11 => Terminating, [11,1,0] |
|||
12 => Terminating, [12,16,15,9,4,3,1,0] |
|||
28 => Perfect, [28] |
|||
496 => Perfect, [496] |
|||
220 => Amicable, [220,284] |
|||
1184 => Amicable, [1184,1210] |
|||
12496 => Sociable, [12496,14288,15472,14536,14264] |
|||
1264460 => Sociable, [1264460,1547860,1727636,1305184] |
|||
790 => Aspiring, [790,650,652,496] |
|||
909 => Aspiring, [909,417,143,25,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,22320,55056,95728,96720,236592,459792,881392,882384,1474608,2461648] |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |