Aliquot sequence classifications: Difference between revisions

Content added Content deleted
(Added Wren)
(→‎{{header|Wren}}: Now uses 'fmt' module.)
Line 3,667: Line 3,667:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|fmt}}
<lang ecmascript>class Classification {
<lang ecmascript>import "/fmt" for Conv, Fmt

class Classification {
construct new(seq, aliquot) {
construct new(seq, aliquot) {
_seq = seq
_seq = seq
Line 3,675: Line 3,678:
aliquot { _aliquot }
aliquot { _aliquot }
}
}

var THRESHOLD = 2.pow(47)
var THRESHOLD = 2.pow(47)

var sumProperDivisors = Fn.new { |n|
var sumProperDivisors = Fn.new { |n|
if (n < 2) return 0
if (n < 2) return 0
Line 3,690: Line 3,693:
return sum
return sum
}
}

var listIndexOf = Fn.new { |lst, search|
var listIndexOf = Fn.new { |lst, search|
for (i in 0...lst.count) {
for (i in 0...lst.count) {
Line 3,697: Line 3,700:
return -1
return -1
}
}

var classifySequence = Fn.new { |k|
var classifySequence = Fn.new { |k|
if (k <= 0) Fiber.abort("K must be positive")
if (k <= 0) Fiber.abort("K must be positive")
Line 3,717: Line 3,720:
}
}
}
}

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")
System.print("Aliquot classifications - periods for Sociable/Cyclic in square brackets:\n")
for (k in 1..10) {
for (k in 1..10) {
var c = classifySequence.call(k)
var c = classifySequence.call(k)
System.print("%(rset.call(2, k)): %(lset.call(15, c.aliquot)) %(c.seq)")
System.print("%(Fmt.d(2, k)): %(Fmt.s(-15, c.aliquot)) %(c.seq)")
}
}

System.print()
System.print()
var a = [11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488]
var a = [11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488]
for (k in a) {
for (k in a) {
var c = classifySequence.call(k)
var c = classifySequence.call(k)
System.print("%(rset.call(7, k)): %(lset.call(15, c.aliquot)) %(c.seq)")
System.print("%(Fmt.d(7, k)): %(Fmt.s(-15, c.aliquot)) %(c.seq)")
}
}

System.print()
System.print()
var k = 15355717786080
var k = 15355717786080
var c = classifySequence.call(k)
var c = classifySequence.call(k)
var seq = c.seq.map { |i| Conv.dec(i) }.toList // ensure 15 digit integer is printed in full
System.print("%(k): %(lset.call(15, c.aliquot)) %(slprint.call(c.seq))")</lang>
System.print("%(k): %(Fmt.s(-15, c.aliquot)) %(seq)")</lang>


{{out}}
{{out}}