Aliquot sequence classifications: Difference between revisions
Content added Content deleted
(Add Swift) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 2,106: | Line 2,106: | ||
1488: non-term., [1488, 2480, 3472, 4464, 8432, 9424, 10416, 21328, 22320, 55056, 95728, 96720, 236592, 459792, 881392, 882384] |
1488: non-term., [1488, 2480, 3472, 4464, 8432, 9424, 10416, 21328, 22320, 55056, 95728, 96720, 236592, 459792, 881392, 882384] |
||
15355717786080: non-term., [15355717786080, 44534663601120]</pre> |
15355717786080: non-term., [15355717786080, 44534663601120]</pre> |
||
=={{header|Phix}}== |
|||
Translated from the Python example |
|||
<lang Phix>function aliquot(atom n) |
|||
sequence s = {n} |
|||
integer k |
|||
if n=0 then return {"terminating",{0}} end if |
|||
while length(s)<16 |
|||
and n<140737488355328 do |
|||
n = sum(factors(n,-1)) |
|||
k = find(n,s) |
|||
if k then |
|||
if k=1 then |
|||
if length(s)=1 then return {"perfect",s} |
|||
elsif length(s)=2 then return {"amicable",s} |
|||
end if return {"sociable",s} |
|||
elsif k=length(s) then return {"aspiring",s} |
|||
end if return {"cyclic",append(s,n)} |
|||
elsif n=0 then return {"terminating",s} |
|||
end if |
|||
s = append(s,n) |
|||
end while |
|||
return {"non-terminating",s} |
|||
end function |
|||
function flat_d(sequence s) |
|||
for i=1 to length(s) do s[i] = sprintf("%d",s[i]) end for |
|||
return join(s,",") |
|||
end function |
|||
constant n = tagset(12)&{28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488, 15355717786080} |
|||
sequence class, dseq |
|||
for i=1 to length(n) do |
|||
{class, dseq} = aliquot(n[i]) |
|||
printf(1,"%14d => %15s, {%s}\n",{n[i],class,flat_d(dseq)}) |
|||
end for</lang> |
|||
{{out}} |
|||
<pre> |
|||
1 => terminating, {1} |
|||
2 => terminating, {2,1} |
|||
3 => terminating, {3,1} |
|||
4 => terminating, {4,3,1} |
|||
5 => terminating, {5,1} |
|||
6 => perfect, {6} |
|||
7 => terminating, {7,1} |
|||
8 => terminating, {8,7,1} |
|||
9 => terminating, {9,4,3,1} |
|||
10 => terminating, {10,8,7,1} |
|||
11 => terminating, {11,1} |
|||
12 => terminating, {12,16,15,9,4,3,1} |
|||
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} |
|||
15355717786080 => non-terminating, {15355717786080,44534663601120,144940087464480} |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 2,235: | Line 2,172: | ||
15355717786080 non-term [15355717786080 44534663601120]</pre> |
15355717786080 non-term [15355717786080 44534663601120]</pre> |
||
=={{header| |
=={{header|Phix}}== |
||
Translated from the Python example |
|||
{{works with|rakudo|2018.10}} |
|||
<lang |
<lang Phix>function aliquot(atom n) |
||
sequence s = {n} |
|||
my @l = x > 1; |
|||
integer k |
|||
(2 .. x.sqrt.floor).map: -> \d { |
|||
if n=0 then return {"terminating",{0}} end if |
|||
while length(s)<16 |
|||
} |
|||
and n<140737488355328 do |
|||
sum @l; |
|||
n = sum(factors(n,-1)) |
|||
} |
|||
k = find(n,s) |
|||
if k then |
|||
if k=1 then |
|||
if length(s)=1 then return {"perfect",s} |
|||
elsif length(s)=2 then return {"amicable",s} |
|||
end if return {"sociable",s} |
|||
elsif k=length(s) then return {"aspiring",s} |
|||
end if return {"cyclic",append(s,n)} |
|||
elsif n=0 then return {"terminating",s} |
|||
end if |
|||
s = append(s,n) |
|||
end while |
|||
return {"non-terminating",s} |
|||
end function |
|||
function flat_d(sequence s) |
|||
multi quality (0,1) { 'perfect ' } |
|||
for i=1 to length(s) do s[i] = sprintf("%d",s[i]) end for |
|||
multi quality (0,2) { 'amicable' } |
|||
return join(s,",") |
|||
multi quality (0,$n) { "sociable-$n" } |
|||
end function |
|||
multi quality ($,1) { 'aspiring' } |
|||
multi quality ($,$n) { "cyclic-$n" } |
|||
constant n = tagset(12)&{28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488, 15355717786080} |
|||
sub aliquotidian ($x) { |
|||
sequence class, dseq |
|||
my %seen; |
|||
for i=1 to length(n) do |
|||
my @seq = $x, &propdivsum ... *; |
|||
{class, dseq} = aliquot(n[i]) |
|||
printf(1,"%14d => %15s, {%s}\n",{n[i],class,flat_d(dseq)}) |
|||
my $this = @seq[$to] or return "$x\tterminating\t[@seq[^$to]]"; |
|||
end for</lang> |
|||
last if $this > 140737488355328; |
|||
if %seen{$this}:exists { |
|||
my $from = %seen{$this}; |
|||
return "$x\t&quality($from, $to-$from)\t[@seq[^$to]]"; |
|||
} |
|||
%seen{$this} = $to; |
|||
} |
|||
"$x non-terminating\t[{@seq}]"; |
|||
} |
|||
aliquotidian($_).say for flat |
|||
1..10, |
|||
11, 12, 28, 496, 220, 1184, 12496, 1264460, |
|||
790, 909, 562, 1064, 1488, |
|||
15355717786080;</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
1 => terminating, {1} |
|||
2 => terminating, {2,1} |
|||
3 => terminating, {3,1} |
|||
4 terminating [4 3 1] |
|||
4 => terminating, {4,3,1} |
|||
5 => terminating, {5,1} |
|||
6 perfect [6] |
|||
6 => perfect, {6} |
|||
7 terminating [7 1] |
|||
7 => terminating, {7,1} |
|||
8 terminating [8 7 1] |
|||
8 => terminating, {8,7,1} |
|||
9 terminating [9 4 3 1] |
|||
9 => terminating, {9,4,3,1} |
|||
10 terminating [10 8 7 1] |
|||
10 => terminating, {10,8,7,1} |
|||
11 => terminating, {11,1} |
|||
12 terminating [12 16 15 9 4 3 1] |
|||
12 => terminating, {12,16,15,9,4,3,1} |
|||
28 perfect [28] |
|||
28 => perfect, {28} |
|||
496 perfect [496] |
|||
496 => perfect, {496} |
|||
220 amicable [220 284] |
|||
220 => amicable, {220,284} |
|||
1184 amicable [1184 1210] |
|||
1184 => amicable, {1184,1210} |
|||
12496 sociable-5 [12496 14288 15472 14536 14264] |
|||
12496 => sociable, {12496,14288,15472,14536,14264} |
|||
1264460 sociable-4 [1264460 1547860 1727636 1305184] |
|||
1264460 => sociable, {1264460,1547860,1727636,1305184} |
|||
790 aspiring [790 650 652 496] |
|||
790 => aspiring, {790,650,652,496} |
|||
909 aspiring [909 417 143 25 6] |
|||
909 => aspiring, {909,417,143,25,6} |
|||
562 cyclic-2 [562 284 220] |
|||
562 => cyclic, {562,284,220,284} |
|||
1064 cyclic-2 [1064 1336 1184 1210] |
|||
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 ...] |
|||
1488 => non-terminating, {1488,2480,3472,4464,8432,9424,10416,21328,22320,55056,95728,96720,236592,459792,881392,882384} |
|||
15355717786080 non-terminating [15355717786080 44534663601120 144940087464480 ...] |
|||
15355717786080 => non-terminating, {15355717786080,44534663601120,144940087464480} |
|||
</pre> |
</pre> |
||
Line 2,716: | Line 2,653: | ||
1488: non-terminating long sequence (1488 2480 3472 4464 8432 9424 10416 21328 22320 55056 95728 96720 236592 459792 881392 882384 1474608) |
1488: non-terminating long sequence (1488 2480 3472 4464 8432 9424 10416 21328 22320 55056 95728 96720 236592 459792 881392 882384 1474608) |
||
15355717786080: non-terminating big number (15355717786080 44534663601120 144940087464480) |
15355717786080: non-terminating big number (15355717786080 44534663601120 144940087464480) |
||
</pre> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|rakudo|2018.10}} |
|||
<lang perl6>sub propdivsum (\x) { |
|||
my @l = x > 1; |
|||
(2 .. x.sqrt.floor).map: -> \d { |
|||
unless x % d { my \y = x div d; y == d ?? @l.push: d !! @l.append: d,y } |
|||
} |
|||
sum @l; |
|||
} |
|||
multi quality (0,1) { 'perfect ' } |
|||
multi quality (0,2) { 'amicable' } |
|||
multi quality (0,$n) { "sociable-$n" } |
|||
multi quality ($,1) { 'aspiring' } |
|||
multi quality ($,$n) { "cyclic-$n" } |
|||
sub aliquotidian ($x) { |
|||
my %seen; |
|||
my @seq = $x, &propdivsum ... *; |
|||
for 0..16 -> $to { |
|||
my $this = @seq[$to] or return "$x\tterminating\t[@seq[^$to]]"; |
|||
last if $this > 140737488355328; |
|||
if %seen{$this}:exists { |
|||
my $from = %seen{$this}; |
|||
return "$x\t&quality($from, $to-$from)\t[@seq[^$to]]"; |
|||
} |
|||
%seen{$this} = $to; |
|||
} |
|||
"$x non-terminating\t[{@seq}]"; |
|||
} |
|||
aliquotidian($_).say for flat |
|||
1..10, |
|||
11, 12, 28, 496, 220, 1184, 12496, 1264460, |
|||
790, 909, 562, 1064, 1488, |
|||
15355717786080;</lang> |
|||
{{out}} |
|||
<pre>1 terminating [1] |
|||
2 terminating [2 1] |
|||
3 terminating [3 1] |
|||
4 terminating [4 3 1] |
|||
5 terminating [5 1] |
|||
6 perfect [6] |
|||
7 terminating [7 1] |
|||
8 terminating [8 7 1] |
|||
9 terminating [9 4 3 1] |
|||
10 terminating [10 8 7 1] |
|||
11 terminating [11 1] |
|||
12 terminating [12 16 15 9 4 3 1] |
|||
28 perfect [28] |
|||
496 perfect [496] |
|||
220 amicable [220 284] |
|||
1184 amicable [1184 1210] |
|||
12496 sociable-5 [12496 14288 15472 14536 14264] |
|||
1264460 sociable-4 [1264460 1547860 1727636 1305184] |
|||
790 aspiring [790 650 652 496] |
|||
909 aspiring [909 417 143 25 6] |
|||
562 cyclic-2 [562 284 220] |
|||
1064 cyclic-2 [1064 1336 1184 1210] |
|||
1488 non-terminating [1488 2480 3472 4464 8432 9424 10416 21328 22320 55056 95728 96720 236592 459792 881392 882384 1474608 ...] |
|||
15355717786080 non-terminating [15355717786080 44534663601120 144940087464480 ...] |
|||
</pre> |
</pre> |
||