Aliquot sequence classifications: Difference between revisions
add link to numberphile video
(add task to aarch64 assembly raspberry pi) |
(add link to numberphile video) |
||
(17 intermediate revisions by 13 users not shown) | |||
Line 31:
* [[Amicable pairs]]
<br><br>
;External links:
* [https://www.youtube.com/watch?v=OtYKDzXwDEE An amazing thing about 276], Numberphile
=={{header|11l}}==
{{trans|Python}}
<
R sum((1 .. (n + 1) I/ 2).filter(x -> @n % x == 0 & @n != x))
Line 72 ⟶ 75:
L(n) [11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488]
V (cls, seq) = aliquot(n)
print(‘#.: #.’.format(cls, seq))</
{{out}}
Line 103 ⟶ 106:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program aliquotSeq64.s */
Line 660 ⟶ 663:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
<pre>
Program 64 bits start
Line 691 ⟶ 694:
=={{header|ALGOL 68}}==
Assumes LONG INT is at least 64 bits, as in Algol 68G.
<
# aliquot sequence classification #
# maximum sequence length we consider #
Line 796 ⟶ 799:
print( ( newline ) )
OD
END</
{{out}}
<pre>
Line 827 ⟶ 830:
=={{header|AppleScript}}==
<
if (n < 2) then return 0
set sum to 1
Line 895 ⟶ 898:
set output to output as text
set AppleScript's text item delimiters to astid
return output</
{{output}}
<
1: terminating 1, 0
2: terminating 2, 1, 0
Line 922 ⟶ 925:
1064: cyclic 1064, 1336, 1184, 1210
1488: non-terminating 1488, 2480, 3472, 4464, 8432, 9424, 10416, 21328, 22320, 55056, 95728, 96720, 236592, 459792, 881392, 882384
1.535571778608E+13: non-terminating 1.535571778608E+13, 4.453466360112E+13, 1.449400874645E+14"</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program aliquotSeq.s */
Line 1,470 ⟶ 1,473:
.include "../affichage.inc"
</syntaxhighlight>
<pre>
Program start
Line 1,500 ⟶ 1,503:
=={{header|AWK}}==
<
#!/bin/gawk -f
function sumprop(num, i,sum,root) {
Line 1,568 ⟶ 1,571:
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,598 ⟶ 1,601:
</pre>
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256"># Rosetta Code problem: http://rosettacode.org/wiki/Aliquot_sequence_classifications
# by Jjuanhdez, 06/2022
global limite
limite = 20000000
dim nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488}
for n = 0 to nums[?]-1
print "Number "; nums[n]; " : ";
call PrintAliquotClassifier(nums[n])
next n
print
print "Program normal end."
end
function PDtotal (n)
total = 0
for y = 2 to n
if (n mod y) = 0 then total += (n / y)
next y
return total
end function
subroutine PrintAliquotClassifier (K)
longit = 52: n = K: clase = 0: priorn = 0: inc = 0
dim Aseq(longit)
for element = 2 to longit
Aseq[element] = PDtotal(n)
n = int(Aseq[element])
print n; " ";
begin case
case n = 0
print " Terminating": clase = 1: exit for
case n = K and element = 2
print " Perfect" : clase = 2: exit for
case n = K and element = 3
print " Amicable": clase = 3: exit for
case n = K and element > 3
print " Sociable": clase = 4: exit for
case n <> K and Aseq[element - 1] = Aseq[element]
print " Aspiring": clase = 5: exit for
case n <> K and Aseq[element - 2] = n
print " Cyclic": clase = 6: exit for
end case
if n > priorn then priorn = n: inc += 1 else inc = 0: priorn = 0
if inc = 11 or n > limite then exit for
next element
if clase = 0 then print " non-terminating"
end subroutine</syntaxhighlight>
=={{header|C}}==
Line 1,603 ⟶ 1,662:
===Brute Force===
The following implementation is a brute force method which takes a very, very long time for 15355717786080. To be fair to C, that's also true for many of the other implementations on this page which also implement the brute force method. See the next implementation for the best solution.
<syntaxhighlight lang="c">
#include<stdlib.h>
#include<string.h>
Line 1,675 ⟶ 1,734:
return 0;
}
</syntaxhighlight>
Input file, you can include 15355717786080 or similar numbers in this list but be prepared to wait for a very, very long time.:
<pre>
Line 1,736 ⟶ 1,795:
===Number Theoretic===
The following implementation, based on Number Theory, is the best solution for such a problem. All cases are handled, including 15355717786080, with all the numbers being processed and the output written to console practically instantaneously. The above brute force implementation is the original one and it remains to serve as a comparison of the phenomenal difference the right approach can make to a problem.
<syntaxhighlight lang="c">
#include<string.h>
#include<stdlib.h>
Line 1,838 ⟶ 1,897:
return 0;
}
</syntaxhighlight>
Input file, to emphasize the effectiveness of this approach, the last number in the file is 153557177860800, 10 times the special case mentioned in the task.
<pre>
Line 1,902 ⟶ 1,961:
336056, 1405725265675144, 1230017019320456, 68719476751
</pre>
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
using System.Linq;
public class AliquotSequenceClassifications
{
private static long ProperDivsSum(long n)
{
return Enumerable.Range(1, (int)(n / 2)).Where(i => n % i == 0).Sum(i => (long)i);
}
public static bool Aliquot(long n, int maxLen, long maxTerm)
{
List<long> s = new List<long>(maxLen) {n};
long newN = n;
while (s.Count <= maxLen && newN < maxTerm)
{
newN = ProperDivsSum(s.Last());
if (s.Contains(newN))
{
if (s[0] == newN)
{
switch (s.Count)
{
case 1:
return Report("Perfect", s);
case 2:
return Report("Amicable", s);
default:
return Report("Sociable of length " + s.Count, s);
}
}
else if (s.Last() == newN)
{
return Report("Aspiring", s);
}
else
{
return Report("Cyclic back to " + newN, s);
}
}
else
{
s.Add(newN);
if (newN == 0)
return Report("Terminating", s);
}
}
return Report("Non-terminating", s);
}
static bool Report(string msg, List<long> result)
{
Console.WriteLine(msg + ": " + string.Join(", ", result));
return false;
}
public static void Main(string[] args)
{
long[] arr = {
11, 12, 28, 496, 220, 1184, 12496, 1264460,
790, 909, 562, 1064, 1488
};
Enumerable.Range(1, 10).ToList().ForEach(n => Aliquot(n, 16, 1L << 47));
Console.WriteLine();
foreach (var n in arr)
{
Aliquot(n, 16, 1L << 47);
}
}
}
</syntaxhighlight>
{{out}}
<pre>
Terminating: 1, 0
Terminating: 2, 1, 0
Terminating: 3, 1, 0
Terminating: 4, 3, 1, 0
Terminating: 5, 1, 0
Perfect: 6
Terminating: 7, 1, 0
Terminating: 8, 7, 1, 0
Terminating: 9, 4, 3, 1, 0
Terminating: 10, 8, 7, 1, 0
Terminating: 11, 1, 0
Terminating: 12, 16, 15, 9, 4, 3, 1, 0
Perfect: 28
Perfect: 496
Amicable: 220, 284
Amicable: 1184, 1210
Sociable of length 5: 12496, 14288, 15472, 14536, 14264
Sociable of length 4: 1264460, 1547860, 1727636, 1305184
Aspiring: 790, 650, 652, 496
Aspiring: 909, 417, 143, 25, 6
Cyclic back to 284: 562, 284, 220
Cyclic back to 1184: 1064, 1336, 1184, 1210
Non-terminating: 1488, 2480, 3472, 4464, 8432, 9424, 10416, 21328, 22320, 55056, 95728, 96720, 236592, 459792, 881392, 882384, 1474608
</pre>
=={{header|C++}}==
This one follows the trail blazed by the "Number Theoretic" C example above.
<
#include <iostream>
#include <string>
Line 1,974 ⟶ 2,143:
classify_aliquot_sequence(153557177860800);
return 0;
}</
{{out}}
Line 2,007 ⟶ 2,176:
=={{header|CLU}}==
{{trans|C++}}
<
% Remove leading and trailing whitespace (bigint$unparse adds a lot)
Line 2,102 ⟶ 2,271:
classify_aliquot_sequence(bigint$parse("15355717786080"))
classify_aliquot_sequence(bigint$parse("153557177860800"))
end start_up</
{{out}}
<pre>1: terminating, sequence: 1 0
Line 2,132 ⟶ 2,301:
=={{header|Common Lisp}}==
Uses the Lisp function '''proper-divisors-recursive''' from [[Proper_divisors#Common_Lisp|Task:Proper Divisors]].
<
(defparameter *klimit* (expt 2 47))
(defparameter *asht* (make-hash-table))
Line 2,198 ⟶ 2,367:
(aliquot (+ k 1)))
(dolist (k '(11 12 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080))
(aliquot k)))</
{{out}}
<pre>CL-USER(45): (main)
Line 2,231 ⟶ 2,400:
=={{header|D}}==
{{trans|Python}}
<
auto properDivisors(in ulong n) pure nothrow @safe /*@nogc*/ {
Line 2,281 ⟶ 2,450:
790, 909, 562, 1064, 1488])
writefln("%s: %s", n.aliquot[]);
}</
{{out}}
<pre>Terminating: [1, 0]
Line 2,307 ⟶ 2,476:
Cyclic back to 1184: [1064, 1336, 1184, 1210]
Non-terminating: [1488, 2480, 3472, 4464, 8432, 9424, 10416, 21328, 22320, 55056, 95728, 96720, 236592, 459792, 881392, 882384, 1474608]</pre>
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight lang=easylang>
fastfunc sumprop num .
if num = 1
return 0
.
sum = 1
root = sqrt num
i = 2
while i < root
if num mod i = 0
sum += i + num / i
.
i += 1
.
if num mod root = 0
sum += root
.
return sum
.
func$ tostr ar[] .
for v in ar[]
s$ &= " " & v
.
return s$
.
func$ class k .
oldk = k
newk = sumprop oldk
oldk = newk
seq[] &= newk
if newk = 0
return "terminating " & tostr seq[]
.
if newk = k
return "perfect " & tostr seq[]
.
newk = sumprop oldk
oldk = newk
seq[] &= newk
if newk = 0
return "terminating " & tostr seq[]
.
if newk = k
return "amicable " & tostr seq[]
.
for t = 4 to 16
newk = sumprop oldk
seq[] &= newk
if newk = 0
return "terminating " & tostr seq[]
.
if newk = k
return "sociable (period " & t - 1 & ") " & tostr seq[]
.
if newk = oldk
return "aspiring " & tostr seq[]
.
for i to len seq[] - 1
if newk = seq[i]
return "cyclic (at " & newk & ") " & tostr seq[]
.
.
if newk > 140737488355328
return "non-terminating (term > 140737488355328) " & tostr seq[]
.
oldk = newk
.
return "non-terminating (after 16 terms) " & tostr seq[]
.
print "Number classification sequence"
for j = 1 to 12
print j & " " & class j
.
for j in [ 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080 ]
print j & " " & class j
.
</syntaxhighlight>
=={{header|EchoLisp}}==
<
;; implementation of Floyd algorithm to find cycles in a graph
;; see Wikipedia https://en.wikipedia.org/wiki/Cycle_detection
Line 2,364 ⟶ 2,613:
(when (= x0 starter) (set! end starter)))
(writeln ...))
</syntaxhighlight>
{{out}}
<
(lib 'math)
(lib 'bigint)
Line 2,406 ⟶ 2,655:
1000 terminating
1000 1340 1516 1144 1376 1396 1054 674 340 416 466 236 184 176 196 203 37 1 0 0 ...
</syntaxhighlight>
=={{header|Elixir}}==
{{trans|Ruby}}
<
def divisors(1), do: []
def divisors(n), do: [1 | divisors(2,n,:math.sqrt(n))] |> Enum.sort
Line 2,459 ⟶ 2,708:
if n<10000000, do: :io.fwrite("~7w:~21s: ~p~n", [n, msg, s]),
else: :io.fwrite("~w: ~s: ~p~n", [n, msg, s])
end)</
{{out}}
Line 2,495 ⟶ 2,744:
=={{header|Factor}}==
For convenience, the term that caused termination is always included in the output sequence.
<
literals locals math math.functions math.primes.factors
math.ranges namespaces pair-rocket sequences sets ;
Line 2,548 ⟶ 2,797:
10 [1,b] test-cases append [ .classify ] each ;
MAIN: main</
{{out}}
<pre>
Line 2,612 ⟶ 2,861:
A more flexible syntax (such as Algol's) would enable the double scan of the TRAIL array to be avoided, as in if TRAIL[I:=MinLoc(Abs(TRAIL(1:L) - SF))] = SF then... That is, find the first index of array TRAIL such that ABS(TRAIL(1:L) - SF) is minimal, save that index in I, then access that element of TRAIL and test if it is equal to SF. The INDEX function could be use to find the first match, except that it is defined only for character variables. Alternatively, use an explicit DO-loop to search for equality, thus not employing fancy syntax, and not having to wonder if the ANY function will stop on the first match rather than wastefully continue the testing for all array elements. The modern style in manual writing is to employ vaguely general talk about arrays and omit specific details.
<syntaxhighlight lang="fortran">
MODULE FACTORSTUFF !This protocol evades the need for multiple parameters, or COMMON, or one shapeless main line...
Concocted by R.N.McLean, MMXV.
Line 2,736 ⟶ 2,985:
END DO
END !Done.
</syntaxhighlight>
=={{header|FreeBASIC}}==
{{trans|C}}
<
dim as ulongint result = 1, i
for i = 1 to power
Line 2,818 ⟶ 3,067:
for n as ubyte = 0 to 22
aliquotClassifier(nums(n))
next n</
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 2,930 ⟶ 3,179:
seq, aliquot := classifySequence(k)
fmt.Printf("%d: %-15s %s\n", k, aliquot, joinWithCommas(seq))
}</
{{out}}
Line 2,965 ⟶ 3,214:
=={{header|Haskell}}==
<
divisors n = filter ((0 ==) . (n `mod`)) [1 .. (n `div` 2)]
Line 3,005 ⟶ 3,254:
let cls n = let ali = take 16 $ aliquot n in (classify ali, ali)
mapM_ (print . cls) $ [1..10] ++
[11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488]</
{{out}}
<pre>(Terminating,[1,0])
Line 3,033 ⟶ 3,282:
=={{header|J}}==
Implementation:
<
aliquot=: +/@proper_divisors ::0:
rc_aliquot_sequence=: aliquot^:(i.16)&>
Line 3,047 ⟶ 3,296:
end.
)
rc_display_aliquot_sequence=: (rc_classify,' ',":)@:rc_aliquot_sequence</
Task example:
<
terminate 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
terminate 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Line 3,076 ⟶ 3,325:
cyclic 1064 1336 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210
non-terminating 1488 2480 3472 4464 8432 9424 10416 21328 22320 55056 95728 96720 236592 459792 881392 882384
non-terminating 15355717786080 44534663601120 144940087464480 471714103310688 1130798979186912 2688948041357088 6050151708497568 13613157922639968 35513546724070632 74727605255142168 162658586225561832 353930992506879768 642678347124409032 1125102611548462968 1977286128289819992 3415126495450394808</
=={{header|Java}}==
Translation of [[Aliquot_sequence_classifications#Python|Python]] via [[Aliquot_sequence_classifications#D|D]]
{{works with|Java|8}}
<
import java.util.Arrays;
import java.util.List;
Line 3,144 ⟶ 3,393:
Arrays.stream(arr).forEach(n -> aliquot(n, 16, 1L << 47));
}
}</
<pre>Terminating: [1, 0]
Line 3,174 ⟶ 3,423:
=={{header|jq}}==
{{works with|jq|1.4}}
<
# than jq 1.4
def until(cond; next):
Line 3,232 ⟶ 3,481:
790, 909, 562, 1064, 1488, 15355717786080) | pp);
task</
{{out}}
<
1: terminating: [1,0]
2: terminating: [2,1,0]
Line 3,259 ⟶ 3,508:
1064: cyclic back to 1184: [1064,1336,1184,1210]
1488: non-terminating: [1488,2480,3472,4464,8432,9424,10416,21328,22320,55056,95728,96720,236592,459792,881392,882384]
15355717786080: non-terminating: [15355717786080,44534663601120]</
=={{header|Julia}}==
'''Core Function'''
<syntaxhighlight lang="julia">
function aliquotclassifier{T<:Integer}(n::T)
a = T[n]
Line 3,288 ⟶ 3,537:
return ("Non-terminating", a)
end
</syntaxhighlight>
'''Supporting Functions'''
<syntaxhighlight lang="julia">
function pcontrib{T<:Integer}(p::T, a::T)
n = one(T)
Line 3,309 ⟶ 3,558:
dsum -= n
end
</syntaxhighlight>
'''Main'''
<
println("Classification Tests:")
Line 3,320 ⟶ 3,569:
println(@sprintf("%8d => ", i), @sprintf("%16s, ", class), a)
end
</syntaxhighlight>
{{out}}
Line 3,351 ⟶ 3,600:
=={{header|Kotlin}}==
<
data class Classification(val sequence: List<Long>, val aliquot: String)
Line 3,412 ⟶ 3,661:
val (seq, aliquot) = classifySequence(k)
println("$k: ${aliquot.padEnd(15)} $seq")
}</
{{out}}
Line 3,462 ⟶ 3,711:
There are no sociable sequences.
<syntaxhighlight lang="lb">
print "ROSETTA CODE - Aliquot sequence classifications"
[Start]
Line 3,507 ⟶ 3,756:
next
end function
</syntaxhighlight>
{{out}}
Line 3,576 ⟶ 3,825:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
NestList[If[# == 0, 0,
DivisorSum[#, # &, Function[div, div != #]]] &, n, 16];
Line 3,594 ⟶ 3,843:
Print[{#, class[seq[#]], notate[seq[#]] /. {0} -> 0}] & /@ {1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909,
562, 1064, 1488, 15355717786080};</
{{out}}
<pre>{1, Terminating, {1, 0}}
Line 3,622 ⟶ 3,871:
=={{header|Nim}}==
<syntaxhighlight lang="nim">import std/[math, strformat, times]
from std/strutils import
type
# Classification categories.
Category {.pure.} = enum
Unknown
Terminating = "terminating"
Line 3,642 ⟶ 3,888:
# Aliquot sequence.
AliquotSeq = seq[
const Limit = 2^47 # Limit beyond which the category is considered to be "NonTerminating".
Line 3,648 ⟶ 3,894:
#---------------------------------------------------------------------------------------------------
proc sumProperDivisors(n:
## Compute the sum of proper divisors.*
if n == 1: return 0
result = 1
for d in 2..sqrt(n.
if n mod d == 0:
inc result, d
if n div d != d:
#---------------------------------------------------------------------------------------------------
iterator aliquotSeq(n:
## Yield the elements of the aliquot sequence of "n".
## Stopped if the current value is null or equal to "n".
Line 3,681 ⟶ 3,927:
#---------------------------------------------------------------------------------------------------
proc classification(n:
## Return the category of the aliquot sequence of a number "n" and the sequence itself.
Line 3,713 ⟶ 3,959:
for n in 1..10:
let (cat, aseq) = classification(n)
echo fmt"{n:14}: {cat:<20} {aseq}"
echo ""
for n in [int64 11, 12, 28, 496, 220, 1184, 12496, 1264460,
790, 909, 562, 1064, 1488, 15355717786080
let (cat, aseq) = classification(n)
echo fmt"{n:14}: {cat:<20} {aseq}"
Line 3,723 ⟶ 3,969:
echo ""
echo fmt"Processed in {(getTime() - t0).inMilliseconds} ms."
</syntaxhighlight>
{{out}}
Line 3,758 ⟶ 4,004:
=={{header|Oforth}}==
<
import: quicksort
import: math
Line 3,794 ⟶ 4,040:
]
"non-terminating"
;</
{{out}}
Line 3,834 ⟶ 4,080:
=={{header|PARI/GP}}==
Define function aliquot(). Works with recent versions of PARI/GP >= 2.8:
<
{
my (L = List(x), M = Map(Mat([x,1])), k, m = "non-term.", n = x);
Line 3,851 ⟶ 4,097:
);
printf("%16d: %10s, %s\n", x, m, Vec(L));
}</
Output:
Line 3,883 ⟶ 4,129:
=={{header|Perl}}==
{{libheader|ntheory}}
<
sub aliquot {
Line 3,918 ⟶ 4,164:
my($class, @seq) = aliquot($n);
printf "%14d %10s [@seq]\n", $n, $class;
}</
{{out}}
<pre> 1 terminates [1 0]
Line 3,948 ⟶ 4,194:
=={{header|Phix}}==
Translated from the Python example
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">aliquot</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">}</span>
Line 3,977 ⟶ 4,223:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%14d => %15s, {%s}\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">classification</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dseq</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 4,004 ⟶ 4,250:
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|Picat}}==
{{trans|C++}}
<syntaxhighlight lang="picat">divisor_sum(N) = R =>
Total = 1,
Power = 2,
% Deal with powers of 2 first
while (N mod 2 == 0)
Total := Total + Power,
Power := Power*2,
N := N div 2
end,
% Odd prime factors up to the square root
P = 3,
while (P*P =< N)
Sum = 1,
Power1 = P,
while (N mod P == 0)
Sum := Sum + Power1,
Power1 := Power1*P,
N := N div P
end,
Total := Total * Sum,
P := P+2
end,
% If n > 1 then it's prime
if N > 1 then
Total := Total*(N + 1)
end,
R = Total.
% See https://en.wikipedia.org/wiki/Aliquot_sequence
aliquot_sequence(N,Limit,Seq,Class) =>
aliquot_sequence(N,Limit,[N],Seq,Class).
aliquot_sequence(_,0,_,Seq,Class) => Seq = [], Class = 'non-terminating'.
aliquot_sequence(_,_,[0|_],Seq,Class) => Seq = [0], Class = terminating.
aliquot_sequence(N,_,[N,N|_],Seq,Class) => Seq = [], Class = perfect.
aliquot_sequence(N,_,[N,_,N|_],Seq,Class) => Seq = [N], Class = amicable.
aliquot_sequence(N,_,[N|S],Seq,Class), membchk(N,S) =>
Seq = [N], Class = sociable.
aliquot_sequence(_,_,[Term,Term|_],Seq,Class) => Seq = [], Class = aspiring.
aliquot_sequence(_,_,[Term|S],Seq,Class), membchk(Term,S) =>
Seq = [Term], Class = cyclic.
aliquot_sequence(N,Limit,[Term|S],Seq,Class) =>
Seq = [Term|Rest],
Sum = divisor_sum(Term),
Term1 is Sum - Term,
aliquot_sequence(N,Limit-1,[Term1,Term|S],Rest,Class).
main =>
foreach (N in [11,12,28,496,220,1184,12496,1264460,790,909,562,1064,1488,15355717786080,153557177860800])
aliquot_sequence(N,16,Seq,Class),
printf("%w: %w, sequence: %w ", N, Class, Seq[1]),
foreach (I in 2..len(Seq), break(Seq[I] == Seq[I-1]))
printf("%w ", Seq[I])
end,
nl
end.
</syntaxhighlight>
{{out}}
<pre>
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>
Line 4,017 ⟶ 4,341:
{{works with|PowerShell|4.0}}<br/>
<b>Simple</b>
<
{
If ( $X -gt 1 )
Line 4,050 ⟶ 4,374:
(1..10).ForEach{ [string]$_ + " is " + ( Classify-AlliquotSequence -Sequence ( Get-AliquotSequence -K $_ -N 16 ) ) }
( 11, 12, 28, 496, 220, 1184, 790, 909, 562, 1064, 1488 ).ForEach{ [string]$_ + " is " + ( Classify-AlliquotSequence -Sequence ( Get-AliquotSequence -K $_ -N 16 ) ) }</
<b>Optimized</b>
<
{
If ( $X -gt 1 )
Line 4,114 ⟶ 4,438:
(1..10).ForEach{ [string]$_ + " is " + ( Classify-AlliquotSequence -Sequence ( Get-AliquotSequence -K $_ -N 16 ) ) }
( 11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488 ).ForEach{ [string]$_ + " is " + ( Classify-AlliquotSequence -Sequence ( Get-AliquotSequence -K $_ -N 16 ) ) }</
{{out}}
<pre>1 is terminating
Line 4,141 ⟶ 4,465:
===Version 3.0===
<syntaxhighlight lang="powershell">
function Get-Aliquot
{
Line 4,232 ⟶ 4,556:
}
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
$oneToTen = 1..10 | Get-Aliquot
$selected = 11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488 | Get-Aliquot
Line 4,239 ⟶ 4,563:
$numbers = $oneToTen, $selected
$numbers
</syntaxhighlight>
{{Out}}
<pre>
Line 4,272 ⟶ 4,596:
{{trans|C++}}
{{works with|SWI Prolog}}
<
divisor_sum(N, Total):-
divisor_sum_prime(N, 2, 2, Total1, 1, N1),
Line 4,340 ⟶ 4,664:
write_aliquot_sequence(N, Sequence, Class),
fail.
main.</
{{out}}
Line 4,372 ⟶ 4,696:
Importing [[Proper_divisors#Python:_From_prime_factors|Proper divisors from prime factors]]:
<
from functools import lru_cache
Line 4,412 ⟶ 4,736:
print()
for n in [11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488, 15355717786080]:
print('%s: %r' % aliquot(n))</
{{out}}
Line 4,440 ⟶ 4,764:
non-terminating: [1488, 2480, 3472, 4464, 8432, 9424, 10416, 21328, 22320, 55056, 95728, 96720, 236592, 459792, 881392, 882384, 1474608]
non-terminating: [15355717786080, 44534663601120, 144940087464480]</pre>
=={{header|QBasic}}==
{{works with|QBasic|1.1}}
{{trans|Liberty BASIC}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION PDtotal! (n!)
DECLARE SUB PrintAliquotClassifier (K!)
CLS
CONST limite = 10000000
DIM nums(22)
DATA 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 28, 496
DATA 220, 1184, 12496, 790, 909, 562, 1064, 1488
FOR n = 1 TO UBOUND(nums)
READ nums(n)
PRINT "Number"; nums(n); " :";
PrintAliquotClassifier (nums(n))
NEXT n
PRINT "Program normal end."
END
FUNCTION PDtotal (n)
total = 0
FOR y = 2 TO n
IF (n MOD y) = 0 THEN total = total + (n / y)
NEXT y
PDtotal = total
END FUNCTION
SUB PrintAliquotClassifier (K)
longit = 52: n = K: clase = 0: priorn = 0: inc = 0
DIM Aseq(longit)
FOR element = 2 TO longit
Aseq(element) = PDtotal(n)
PRINT Aseq(element); " ";
COLOR 3
SELECT CASE Aseq(element)
CASE 0
PRINT " Terminating": clase = 1: EXIT FOR
CASE K AND element = 2
PRINT " Perfect": clase = 2: EXIT FOR
CASE K AND element = 3
PRINT " Amicable": clase = 3: EXIT FOR
CASE K AND element > 3
PRINT " Sociable": clase = 4: EXIT FOR
CASE Aseq(element) <> K AND Aseq(element - 1) = Aseq(element)
PRINT " Aspiring": clase = 5: EXIT FOR
CASE Aseq(element) <> K AND Aseq(element - 2) = Aseq(element)
PRINT " Cyclic": clase = 6: EXIT FOR
END SELECT
COLOR 7
n = Aseq(element)
IF n > priorn THEN priorn = n: inc = inc + 1 ELSE inc = 0: priorn = 0
IF inc = 11 OR n > limite THEN EXIT FOR
NEXT element
IF clase = 0 THEN COLOR 12: PRINT " non-terminating"
COLOR 7
END SUB</syntaxhighlight>
{{out}}
<pre>Number 1 : 0 Terminating
Number 2 : 1 0 Terminating
Number 3 : 1 0 Terminating
Number 4 : 3 1 0 Terminating
Number 5 : 1 0 Terminating
Number 6 : 6 Perfect
Number 7 : 1 0 Terminating
Number 8 : 7 1 0 Terminating
Number 9 : 4 3 1 0 Terminating
Number 10 : 8 7 1 0 Terminating
Number 11 : 1 0 Terminating
Number 12 : 16 15 9 4 3 1 0 Terminating
Number 28 : 28 Perfect
Number 496 : 496 Perfect
Number 220 : 284 220 Amicable
Number 1184 : 1210 1184 Amicable
Number 12496 : 14288 15472 14536 14264 12496 Sociable
Number 790 : 650 652 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 496 non-terminating
Number 909 : 417 143 25 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 non-terminating
Number 562 : 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 220 284 non-terminating
Number 1064 : 1336 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 non-terminating
Number 1488 : 2480 3472 4464 8432 9424 10416 21328 22320 55056 95728 96720 non-terminating
Program normal end.</pre>
=={{header|Racket}}==
Line 4,445 ⟶ 4,854:
'''fold-divisors''' is used from [[Proper_divisors#Racket]], but for the truly big numbers, we use divisors from math/number-theory.
<
(require "proper-divisors.rkt" math/number-theory)
Line 4,497 ⟶ 4,906:
(for ((i (in-list '(11 12 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080))))
(report-aliquot-sequence-class i))</
{{out}}
Line 4,529 ⟶ 4,938:
(formerly Perl 6)
{{works with|rakudo|2018.10}}
<syntaxhighlight lang="raku"
my @l = x > 1;
(2 .. x.sqrt.floor).map: -> \d {
Line 4,562 ⟶ 4,971:
11, 12, 28, 496, 220, 1184, 12496, 1264460,
790, 909, 562, 1064, 1488,
15355717786080;</
{{out}}
<pre>1 terminating [1]
Line 4,600 ⟶ 5,009:
Both of the above limitations are imposed by this Rosetta Code task's restriction requirements: ''For the purposes of this task, ···''.
<
high=
low=word(low 1,1) /*obtain the LOW and HIGH (range). */
If LL='' Then
LL=11 12 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080
big=2**47
Numeric Digits max(9,length(big)) /*be able To handle big numbers For // */
digs=digits()
dsum.=.
dsum.0=0
dsum.1=0 /* dsum. are the proper divisor sums. */
Say 'Numbers from ' low ' ---> ' high ' (inclusive):'
Do n=low To high
Call classify n
End
Say
Say 'First numbers for each classification:'
class.=0
Call classify -q /*minus (-) sign indicates don't tell. */
_=translate(what)
class._=class._+1
If class._==1 Then
Call out q,what,dd
End
Say 'Classifications for specific numbers:'
Do i=1 To words(LL)
Call classify
End
Exit
out:
Parse arg number,class,dd
dd.=''
Do di=1 By 1 While length(dd)>50
do dj=50 To 10 By -1
If substr(dd,dj,1)=' ' Then Leave
End
dd.di=left(dd,dj)
dd=substr(dd,dj+1)
End
dd.di=dd
Say right(number,digs)':' center(class,digs) dd.1||conti(1)
Do di=2 By 1 While dd.di>''
Say copies(' ',33)dd.di||conti(di)
End
Return
conti:
Parse arg this
next=this+1
If dd.next>'' Then Return '...'
Else Return ''
/*---------------------------------------------------------------------------------*/
classify:
Parse Arg a 1 aa
a=abs(a)
If dsum.a\==. Then
Else
dsum.a=s /*define sum of the proper divisors. */
dd=s /*define the start of integer sequence.*/
what='terminating' /*assume this kind of classification. */
c.=0 /*clear all cyclic sequences (to zero).*/
c.s=1 /*set the first cyclic sequence. */
If dd==a Then
what='perfect' /*check For a "perfect" number. */
Else Do t=1 By 1 While s>0 /*loop Until sum isn't 0 or > big.*/
m=s /*obtain the last number in sequence. */
If dsum.m==. Then /*Not defined? */
s=dsum(m) /* compute sum pro of per divisors */
Else
s=dsum.m /*use the previously found integer. */
If m==s Then
If m>=0 Then Do
what='aspiring'
Leave
End
If word(dd,2)=a Then Do
what='amicable'
Leave
End
dd=dd s /*append a sum To the integer sequence.*/
If s==a Then
If t>3 Then Do
what='sociable'
Leave
End
If c.s Then
If m>0 Then Do
what='cyclic'
Leave
End
c.s=1 /*assign another possible cyclic number*/
/* [?] Rosetta Code task's limit: >16 */
If t>NTlimit Then Do
what='non-terminating'
Leave
End
If s>big Then Do
what='NON-TERMINATING'
Leave
End
End
If aa>0 Then /* display only if AA is positive */
Call out a,what,dd
Return
/*---------------------------------------------------------------------------------*/
dsum: Procedure Expose dsum. /* compute the sum of proper divisors */
Parse Arg x
If x<2 Then
Return 0
odd=x//2
s=1 /* use EVEN or ODD integers. */
Do j=2+odd by 1+odd While j*j<x /* divide by all the integers ) */
/* up to but excluding sqrt(x) */
If x//j==0 Then /* j is a divisor, so is x%j */
s=s+j+x%j /*add the two divisors To the sum. */
End
If j*j==x Then /* if x is a square */
s=s+j /* add sqrt(X) */
dsum.x=s /* memoize proper divisor sum of X */
Return s /* return the proper divisor sum */
</syntaxhighlight>
{{out|output|text= when using the default input:}}
<pre>Numbers from 1 ---> 10 (inclusive):
1: terminating 0
2: terminating 1 0
Line 4,679 ⟶ 5,149:
10: terminating 8 7 1 0
First numbers for each classification:
1: terminating 0
6: perfect 6
25: aspiring 6
138:
13254 13830 19434 20886 21606 25098 26742 26754
220: amicable 284 220
562: cyclic 284 220 284
12496: sociable 14288 15472 14536 14264 12496
Classifications for specific numbers:
11: terminating 1 0
12: terminating 16 15 9 4 3 1 0
Line 4,701 ⟶ 5,172:
562: cyclic 284 220 284
1064: cyclic 1336 1184 1210 1184
1488:
95728 96720 236592 459792 881392 882384 1474608 ...
2461648 3172912 3173904
15355717786080: NON-TERMINATING 44534663601120 144940087464480
</pre>
=={{header|Ring}}==
<
# Project : Aliquot sequence classnifications
Line 4,785 ⟶ 5,259:
next
return pdtotal
</syntaxhighlight>
Output:
<pre>
Line 4,850 ⟶ 5,324:
Enter an integer:
Program complete.
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
≪ DIVIS DUP SIZE
'''IF''' DUP 2 ≤ '''THEN''' 1 - NIP '''ELSE''' 1 SWAP 1 - SUB ∑LIST '''END'''
R→I
≫ ≫ ‘<span style="color:blue">∑PFAC</span>’ STO
≪ 16 2 47 ^ → smax vmax
≪ { } OVER +
'''DO'''
SWAP <span style="color:blue">∑PFAC</span> SWAP OVER +
'''UNTIL''' OVER NOT LASTARG vmax ≥ OR OVER SIZE smax ≥ OR
'''END''' NIP
≫ ≫ ‘<span style="color:blue">ALIQT</span>’ STO
≪ <span style="color:blue">ALIQT</span> DUP HEAD → seq k
≪ '''CASE'''
seq 0 POS '''THEN''' "terminating" '''END'''
seq 2 GET k == '''THEN''' "perfect" '''END'''
seq 3 GET k == '''THEN''' "amicable" '''END'''
seq 4 OVER SIZE SUB k POS '''THEN''' "sociable" '''END'''
seq ΔLIST 0 POS '''THEN''' "aspiring" '''END'''
seq SORT ΔLIST 0 POS '''THEN''' "cyclic" '''END'''
"non-terminating"
'''END'''
≫ ≫ ‘<span style="color:blue">ALIQLASS</span>’ STO
≪ n <span style="color:blue">ALIQLASS</span> ≫ 'n' 1 10 1 SEQ
{11 12 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080} 1 ≪ <span style="color:blue">ALIQLASS</span> ≫ DOLIST
{{out}}
<pre>
2: { "terminating" "terminating" "terminating" "terminating" "terminating" "terminating" "perfect" "terminating" "terminating" "terminating" }
1: { "terminating" "terminating" "perfect" "perfect" "amicable" "amicable" "sociable" "sociable" "aspiring" "aspiring" "cyclic" "cyclic" "non-terminating" "non-terminating" }
</pre>
Line 4,856 ⟶ 5,366:
{{trans|Python}}
<
return "terminating", [0] if n == 0
s = []
Line 4,884 ⟶ 5,394:
for n in [11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488, 15355717786080]
puts "%20s: %p" % aliquot(n)
end</
{{out}}
Line 4,916 ⟶ 5,426:
=={{header|Rust}}==
<
enum AliquotType { Terminating, Perfect, Amicable, Sociable, Aspiring, Cyclic, NonTerminating }
Line 4,960 ⟶ 5,470:
println!("{} {:?}", num, classify_aliquot(*num));
}
}</
{{out}}
<pre>
Line 4,990 ⟶ 5,500:
=={{header|Scala}}==
Put [[proper_divisors#Scala]] the full /Proper divisors for big (long) numbers/ section to the beginning:
<
val sum = properDivisors(n).sum
if (sum == 0) ("terminate", list ::: List(sum))
Line 5,009 ⟶ 5,519:
val result = numbers.map(i => createAliquotSeq(i, 0, List(i)))
result foreach { v => println(f"${v._2.head}%14d ${v._1}%10s [${v._2 mkString " "}]" ) }</
{{out}}
Line 5,039 ⟶ 5,549:
=={{header|Swift}}==
<
@inlinable
public func factors(sorted: Bool = true) -> [Self] {
Line 5,105 ⟶ 5,615:
print()
print("\(15355717786080): \(classifySequence(k: 15355717786080))")</
{{out}}
Line 5,140 ⟶ 5,650:
This solution creates an iterator from a coroutine to generate aliquot sequences. al_classify uses a "RESULT" exception to achieve some unusual control flow.
<
if {$n == 1} {return 0}
set divs 1
Line 5,212 ⟶ 5,722:
puts [time {
puts [format "%8d -> %-16s : %s" $i {*}[al_classify $i]]
}]</
{{out}}
Line 5,246 ⟶ 5,756:
=={{header|VBA}}==
<
Private Type Aliquot
Line 5,312 ⟶ 5,822:
SumPDiv = t
End Function
</syntaxhighlight>
{{out}}
Line 5,338 ⟶ 5,848:
Aliquot seq of 1064 : Cyclic 1064, 1336, 1184, 1210, 1184
Aliquot seq of 1488 : non-terminating 1488, 2480, 3472, 4464, 8432, 9424, 10416, 21328, 22320, 55056, 95728, 96720, 236592, 459792, 881392, 882384
</pre>
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import math
const threshold = u64(1) << 47
fn index_of(s []u64, search u64) int {
for i, e in s {
if e == search {
return i
}
}
return -1
}
fn contains(s []u64, search u64) bool {
return index_of(s, search) > -1
}
fn max_of(i1 int, i2 int) int {
if i1 > i2 {
return i1
}
return i2
}
fn sum_proper_divisors(n u64) u64 {
if n < 2 {
return 0
}
sqrt := u64(math.sqrt(f64(n)))
mut sum := u64(1)
for i := u64(2); i <= sqrt; i++ {
if n % i != 0 {
continue
}
sum += i + n / i
}
if sqrt * sqrt == n {
sum -= sqrt
}
return sum
}
fn classify_sequence(k u64) ([]u64, string) {
if k == 0 {
panic("Argument must be positive.")
}
mut last := k
mut seq := []u64{}
seq << k
for {
last = sum_proper_divisors(last)
seq << last
n := seq.len
mut aliquot := ""
match true {
last == 0 {
aliquot = "Terminating"
}
n == 2 && last == k {
aliquot = "Perfect"
}
n == 3 && last == k {
aliquot = "Amicable"
}
n >= 4 && last == k {
aliquot = "Sociable[${n-1}]"
}
last == seq[n - 2] {
aliquot = "Aspiring"
}
contains(seq[1 .. max_of(1, n - 2)], last) {
aliquot = "Cyclic[${n - 1 - index_of(seq, last)}]"
}
n == 16 || last > threshold {
aliquot = "Non-Terminating"
}
else {}
}
if aliquot != "" {
return seq, aliquot
}
}
return seq, ''
}
fn main() {
println("Aliquot classifications - periods for Sociable/Cyclic in square brackets:\n")
for k := u64(1); k <= 10; k++ {
seq, aliquot := classify_sequence(k)
println("${k:2}: ${aliquot:-15} $seq")
}
println('')
s := [
u64(11), 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488,
]
for k in s {
seq, aliquot := classify_sequence(k)
println("${k:7}: ${aliquot:-15} $seq")
}
println('')
k := u64(15355717786080)
seq, aliquot := classify_sequence(k)
println("$k: ${aliquot:-15} $seq")
}</syntaxhighlight>
{{out}}
<pre>
Aliquot classifications - periods for Sociable/Cyclic in square brackets:
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, 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, 28]
496: Perfect [496, 496]
220: Amicable [220, 284, 220]
1184: Amicable [1184, 1210, 1184]
12496: Sociable[5] [12496, 14288, 15472, 14536, 14264, 12496]
1264460: Sociable[4] [1264460, 1547860, 1727636, 1305184, 1264460]
790: Aspiring [790, 650, 652, 496, 496]
909: Aspiring [909, 417, 143, 25, 6, 6]
562: Cyclic[2] [562, 284, 220, 284]
1064: Cyclic[2] [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>
Line 5,345 ⟶ 5,995:
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
<
import "./math" for Int, Nums
import "./seq" for Lst
class Classification {
Line 5,383 ⟶ 6,033:
for (k in 1..10) {
var c = classifySequence.call(k)
}
Line 5,390 ⟶ 6,040:
for (k in a) {
var c = classifySequence.call(k)
}
Line 5,397 ⟶ 6,047:
var c = classifySequence.call(k)
var seq = c.seq.map { |i| Conv.dec(i) }.toList // ensure 15 digit integer is printed in full
{{out}}
Line 5,430 ⟶ 6,080:
15355717786080: Non-terminating [15355717786080, 44534663601120, 144940087464480]
</pre>
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Aliquot_sequence_classifications
// by Galileo, 05/2022
sub sumFactors(n)
local i, s
for i = 1 to n / 2
if not mod(n, i) s = s + i
next
return s
end sub
sub printSeries(arr(), size, ty$)
local i
print "Integer: ", arr(0), ", Type: ", ty$, ", Series: ";
for i=0 to size-2
print arr(i), " ";
next i
print
end sub
sub alliquot(n)
local arr(16), i, j, ty$
ty$ = "Sociable"
arr(0) = n
for i = 1 to 15
arr(i) = sumFactors(arr(i-1))
if arr(i)=0 or arr(i)=n or (arr(i) = arr(i-1) and arr(i)<>n) then
if arr(i) = 0 then
ty$ = "Terminating"
elsif arr(i) = n and i = 1 then
ty$ = "Perfect"
elsif arr(i) = n and i = 2 then
ty$ = "Amicable"
elsif arr(i) = arr(i-1) and arr(i)<>n then
ty$ = "Aspiring"
end if
printSeries(arr(),i+1,ty$)
return
end if
for j = 1 to i-1
if arr(j) = arr(i) then
printSeries(arr(),i+1,"Cyclic")
return
end if
next j
next i
printSeries(arr(),i+1,"Non-Terminating")
end sub
data 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 28, 496, 220, 1184
data 12496, 1264460, 790, 909, 562, 1064, 1488, 0
do
read n
if not n break
alliquot(n)
loop</syntaxhighlight>
{{out}}
<pre>Integer: 1, Type: Terminating, Series: 1
Integer: 2, Type: Terminating, Series: 2 1
Integer: 3, Type: Terminating, Series: 3 1
Integer: 4, Type: Terminating, Series: 4 3 1
Integer: 5, Type: Terminating, Series: 5 1
Integer: 6, Type: Perfect, Series: 6
Integer: 7, Type: Terminating, Series: 7 1
Integer: 8, Type: Terminating, Series: 8 7 1
Integer: 9, Type: Terminating, Series: 9 4 3 1
Integer: 10, Type: Terminating, Series: 10 8 7 1
Integer: 11, Type: Terminating, Series: 11 1
Integer: 12, Type: Terminating, Series: 12 16 15 9 4 3 1
Integer: 28, Type: Perfect, Series: 28
Integer: 496, Type: Perfect, Series: 496
Integer: 220, Type: Amicable, Series: 220 284
Integer: 1184, Type: Amicable, Series: 1184 1210
Integer: 12496, Type: Sociable, Series: 12496 14288 15472 14536 14264
Integer: 1264460, Type: Sociable, Series: 1264460 1547860 1727636 1305184
Integer: 790, Type: Aspiring, Series: 790 650 652 496
Integer: 909, Type: Aspiring, Series: 909 417 143 25 6
Integer: 562, Type: Cyclic, Series: 562 284 220
Integer: 1064, Type: Cyclic, Series: 1064 1336 1184 1210
Integer: 1488, Type: Non-Terminating, Series: 1488 2480 3472 4464 8432 9424 10416 21328 22320 55056 95728 96720 236592 459792 881392 882384
---Program done, press RETURN---</pre>
=={{header|zkl}}==
<
fcn aliquot(k){ //-->Walker
Walker(fcn(rk){ k:=rk.value; if(k)rk.set(properDivs(k).sum()); k }.fp(Ref(k)))
}(10).walk(15).println();</
Or, refactoring to remove saving the intermediate divisors (and adding white space):
<
Walker(fcn(rk){
k:=rk.value;
Line 5,445 ⟶ 6,184:
k
}.fp(Ref(k)))
}(10).walk(15).println();</
<
const MAX=(2).pow(47); // 140737488355328
ak,aks:=aliquot(k), ak.walk(16);
Line 5,467 ⟶ 6,206:
else "non-terminating" )
+ " " + aks.filter();
}</
<
T(11,12,28,496,220,1184,12496,1264460,790,909,562,1064,1488)
.pump(fcn(k){ "%6d is %s".fmt(k,classify(k)).println() });</
{{out}}
<pre>
Line 5,503 ⟶ 6,242:
{{trans|AWK}}
This program is correct. However, a bug in the ROM of the ZX Spectrum makes the number 909 of an erroneous result. However, the same program running on Sam BASIC (a superset of Sinclair BASIC that ran on the computer Sam Coupé) provides the correct results.
<
20 INPUT "Enter a number (0 to end): ";k: IF k>0 THEN GO SUB 2000: PRINT k;" ";s$: GO TO 20
40 STOP
Line 5,543 ⟶ 6,282:
2320 NEXT t
2330 LET s$="non-terminating (after 16 terms)"+s$
2340 RETURN</
{{out}}
<pre>Number classification sequence
|