Aliquot sequence classifications: Difference between revisions

Content added Content deleted
(add FreeBASIC)
(Added AppleScript.)
Line 167: Line 167:
15355717786080: non terminating: 44534663601120 144940087464480
15355717786080: non terminating: 44534663601120 144940087464480
</pre>
</pre>

=={{header|AppleScript}}==

<lang applescript>on aliquotSum(n)
set sum to (n > 1) as integer
if (n > 1) then
set sqrt to n ^ 0.5
if (sqrt mod 1 is 0) then
set sum to sum + sqrt as integer
set sqrt to sqrt - 1
end if
repeat with i from (sqrt div 1) to 2 by -1
if (n mod i is 0) then set sum to sum + i + n div i
end repeat
end if
return sum
end aliquotSum

on aliquotSequence(k, maxLength, maxN)
-- Generate the sequence within the specified limitations.
set sequence to {k}
set n to k
repeat (maxLength - 1) times
set n to aliquotSum(n)
set repetition to (sequence contains n)
if (repetition) then exit repeat
set end of sequence to n
if ((n = 0) or (n > maxN)) then exit repeat
end repeat
-- Analyse it.
set sequenceLength to (count sequence)
if (sequenceLength is 1) then
set classification to "perfect"
else if (sequence ends with 0) then
set classification to "terminating"
else if (n = k) then
if (sequenceLength is 2) then
set classification to "amicable"
else
set classification to "sociable"
end if
else if (repetition) then
if (sequence ends with n) then
set classification to "aspiring"
else
set classification to "cyclic"
end if
else
set classification to "non-terminating"
end if
return {sequence:sequence, classification:classification}
end aliquotSequence

-- Task code:
local output, maxLength, maxN, spacing, astid, k
set output to {""}
set {maxLength, maxN} to {16, 2 ^ 47}
set spacing to " "
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
repeat with k in {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ¬
11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488, 1.535571778608E+13}
set thisResult to aliquotSequence(k's contents, maxLength, maxN)
set end of output to text -18 thru -1 of (spacing & k) & ": " & ¬
text 1 thru 17 of (thisResult's classification & spacing) & thisResult's sequence
end repeat
set AppleScript's text item delimiters to linefeed
set output to output as text
set AppleScript's text item delimiters to astid
return output</lang>

{{output}}
<lang applescript>"
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
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
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
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"</lang>


=={{header|AWK}}==
=={{header|AWK}}==