Aliquot sequence classifications: Difference between revisions

Added Wren
m (Minor edit to Prolog code)
(Added Wren)
Line 3,663:
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|Wren}}==
{{trans|Kotlin}}
<lang ecmascript>class Classification {
construct new(seq, aliquot) {
_seq = seq
_aliquot = aliquot
}
seq { _seq}
aliquot { _aliquot }
}
 
var THRESHOLD = 2.pow(47)
 
var sumProperDivisors = Fn.new { |n|
if (n < 2) return 0
var sqrt = n.sqrt.floor
var sum = 1
if (sqrt >= 2) {
sum = (2..sqrt).
where { |i| n % i == 0 }.
reduce(1) { |acc, i| acc + i + (n/i).floor }
}
if (sqrt * sqrt == n) sum = sum - sqrt
return sum
}
 
var listIndexOf = Fn.new { |lst, search|
for (i in 0...lst.count) {
if (lst[i] == search) return i
}
return -1
}
 
var classifySequence = Fn.new { |k|
if (k <= 0) Fiber.abort("K must be positive")
var last = k
var seq = [k]
while (true) {
last = sumProperDivisors.call(last)
seq.add(last)
var n = seq.count
var aliquot =
(last == 0) ? "Terminating" :
(n == 2 && last == k) ? "Perfect" :
(n == 3 && last == k) ? "Amicable" :
(n >= 4 && last == k) ? "Sociable[%(n-1)]" :
(last == seq[n-2]) ? "Aspiring" :
(n > 3 && seq[1..n-3].contains(last)) ? "Cyclic[%(n-1-listIndexOf.call(seq, last))]" :
(n == 16 || last > THRESHOLD) ? "Non-terminating" : ""
if (aliquot != "") return Classification.new(seq, aliquot)
}
}
 
var lset = Fn.new { |m, n|
var s = "%(n)"
var c = s.count
return (m > c) ? s + " " * (m - c) : s
}
 
var rset = Fn.new { |m, n|
var s = "%(n)"
var c = s.count
return (m > c) ? " " * (m - c) + s : s
}
 
// ensures integers up to 2^53 - 1 are expressed in decimal, not scientific notation
var slprint = Fn.new { |a|
var c = a.count
if (c == 0) return a
var res = List.filled(c, "")
var limit = 2.pow(53)
for (i in 0...a.count) {
var s = a[i]
if (s.isInteger && s >= 1e14 && s < limit) {
res[i] = "%((s/100).floor)%(s%100)"
} else {
res[i] = "%(s)"
}
}
return "[" + res.join(", ") + "]"
}
 
System.print("Aliquot classifications - periods for Sociable/Cyclic in square brackets:\n")
for (k in 1..10) {
var c = classifySequence.call(k)
System.print("%(rset.call(2, k)): %(lset.call(15, c.aliquot)) %(c.seq)")
}
 
System.print()
var a = [11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488]
for (k in a) {
var c = classifySequence.call(k)
System.print("%(rset.call(7, k)): %(lset.call(15, c.aliquot)) %(c.seq)")
}
 
System.print()
var k = 15355717786080
var c = classifySequence.call(k)
System.print("%(k): %(lset.call(15, c.aliquot)) %(slprint.call(c.seq))")</lang>
 
{{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>
 
9,482

edits