Aliquot sequence classifications: Difference between revisions

Used "int64" instead of "int" for aliquots. Some other minor changes.
m (BASIC256 moved to the BASIC section.)
(Used "int64" instead of "int" for aliquots. Some other minor changes.)
Line 3,678:
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import std/[math, strformat, times]
from std/strutils import mathaddSep
import strformat
from strutils import addSep
import times
 
type
 
# Classification categories.
Category {.pure.} = enum
Unknown
Terminating = "terminating"
Line 3,698 ⟶ 3,695:
 
# Aliquot sequence.
AliquotSeq = seq[intint64]
 
const Limit = 2^47 # Limit beyond which the category is considered to be "NonTerminating".
Line 3,704 ⟶ 3,701:
#---------------------------------------------------------------------------------------------------
 
proc sumProperDivisors(n: intint64): intint64 =
## Compute the sum of proper divisors.*
 
if n == 1: return 0
result = 1
for d in 2..sqrt(n.toFloatfloat).int:
if n mod d == 0:
inc result, d
if n div d != d:
inc result, += n div d
 
#---------------------------------------------------------------------------------------------------
 
iterator aliquotSeq(n: intint64): intint64 =
## Yield the elements of the aliquot sequence of "n".
## Stopped if the current value is null or equal to "n".
Line 3,737 ⟶ 3,734:
#---------------------------------------------------------------------------------------------------
 
proc classification(n: intint64): tuple[cat: Category, values: AliquotSeq] =
## Return the category of the aliquot sequence of a number "n" and the sequence itself.
 
Line 3,769 ⟶ 3,766:
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.int]:
let (cat, aseq) = classification(n)
echo fmt"{n:14}: {cat:<20} {aseq}"
256

edits