Aliquot sequence classifications: Difference between revisions

Content added Content deleted
m (BASIC256 moved to the BASIC section.)
(Used "int64" instead of "int" for aliquots. Some other minor changes.)
Line 3,678: Line 3,678:


=={{header|Nim}}==
=={{header|Nim}}==
<syntaxhighlight lang="nim">
<syntaxhighlight lang="nim">import std/[math, strformat, times]
import math
from std/strutils import addSep
import strformat
from strutils import addSep
import times


type
type


# Classification categories.
# Classification categories.
Category = enum
Category {.pure.} = enum
Unknown
Unknown
Terminating = "terminating"
Terminating = "terminating"
Line 3,698: Line 3,695:


# Aliquot sequence.
# Aliquot sequence.
AliquotSeq = seq[int]
AliquotSeq = seq[int64]


const Limit = 2^47 # Limit beyond which the category is considered to be "NonTerminating".
const Limit = 2^47 # Limit beyond which the category is considered to be "NonTerminating".
Line 3,704: Line 3,701:
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------


proc sumProperDivisors(n: int): int =
proc sumProperDivisors(n: int64): int64 =
## Compute the sum of proper divisors.*
## Compute the sum of proper divisors.*


if n == 1: return 0
if n == 1: return 0
result = 1
result = 1
for d in 2..sqrt(n.toFloat).int:
for d in 2..sqrt(n.float).int:
if n mod d == 0:
if n mod d == 0:
inc result, d
inc result, d
if n div d != d:
if n div d != d:
inc result, n div d
result += n div d


#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------


iterator aliquotSeq(n: int): int =
iterator aliquotSeq(n: int64): int64 =
## Yield the elements of the aliquot sequence of "n".
## Yield the elements of the aliquot sequence of "n".
## Stopped if the current value is null or equal to "n".
## Stopped if the current value is null or equal to "n".
Line 3,737: Line 3,734:
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------


proc classification(n: int): tuple[cat: Category, values: AliquotSeq] =
proc classification(n: int64): tuple[cat: Category, values: AliquotSeq] =
## Return the category of the aliquot sequence of a number "n" and the sequence itself.
## Return the category of the aliquot sequence of a number "n" and the sequence itself.


Line 3,769: Line 3,766:
for n in 1..10:
for n in 1..10:
let (cat, aseq) = classification(n)
let (cat, aseq) = classification(n)
echo fmt"{n:14}: {cat:<20} {aseq}"
echo fmt"{n:14}: {cat:<20} {aseq}"


echo ""
echo ""
for n in [11, 12, 28, 496, 220, 1184, 12496, 1264460,
for n in [int64 11, 12, 28, 496, 220, 1184, 12496, 1264460,
790, 909, 562, 1064, 1488, 15355717786080.int]:
790, 909, 562, 1064, 1488, 15355717786080]:
let (cat, aseq) = classification(n)
let (cat, aseq) = classification(n)
echo fmt"{n:14}: {cat:<20} {aseq}"
echo fmt"{n:14}: {cat:<20} {aseq}"