Aliquot sequence classifications: Difference between revisions

Content added Content deleted
m (→‎{{header|Wren}}: Minor tidy)
(Add C# implementation)
Line 1,958: Line 1,958:
336056, 1405725265675144, 1230017019320456, 68719476751
336056, 1405725265675144, 1230017019320456, 68719476751
</pre>
</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++}}==
=={{header|C++}}==