Factor-perfect numbers: Difference between revisions

Content added Content deleted
(formatting)
(Added Wren)
Line 179: Line 179:


OEIS A163272: 0, 1, 48, 1280, 2496, 28672, 29808, 454656,
OEIS A163272: 0, 1, 48, 1280, 2496, 28672, 29808, 454656,
</pre>

=={{header|Wren}}==
{{trans|Python}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
A bit slow on the last part - 76 seconds compared to 46 seconds for Python - so I haven't attempted the stretch goal.
<syntaxhighlight lang="ecmascript">import "./math" for Int
import "./fmt" for Fmt

// Uses the first definition and recursion to generate the sequences.
var moreMultiples
moreMultiples = Fn.new { |toSeq, fromSeq|
var oneMores = []
for (i in fromSeq) {
if (i > toSeq[-1] && i%toSeq[-1] == 0) oneMores.add(toSeq + [i])
}
if (oneMores.isEmpty) return []
for (i in 0...oneMores.count) {
oneMores.addAll(moreMultiples.call(oneMores[i], fromSeq))
}
return oneMores
}

// Get factorization sequence count.
var countMultipleSequences = Fn.new { |n|
return moreMultiples.call([1], Int.properDivisors(n)).count + 1
}

var listing = moreMultiples.call([1], Int.properDivisors(48))
listing.add([1, 48])
listing.sort { |l1, l2|
var c1 = l1.count
var c2 = l2.count
for (i in 1...c1.min(c2)) {
if (l1[i] < l2[i]) return true
if (l1[i] > l2[i]) return false
}
if (c1 < c2) return true
return false
}
System.print("%(listing.count) sequences using first definition:")
Fmt.tprint("$-17n", listing, 4)

System.print("\n%(listing.count) sequences using second definition:")
var listing2 = []
for (i in 0...listing.count) {
var seq = listing[i]
if (seq[-1] != 48) seq.add(48)
var seq2 = (1...seq.count).map { |i| (seq[i]/seq[i-1]).floor }.toList
listing2.add(seq2)
}
Fmt.tprint("$-17n", listing2, 4)

System.print("\nOEIS A163272:")
var n = 0
var fpns = []
while (fpns.count < 7) {
if (n == 0 || countMultipleSequences.call(n) == n) fpns.add(n)
n = n + 1
}
System.print(fpns)</syntaxhighlight>

{{out}}
<pre>
48 sequences using first definition:
[1, 2] [1, 2, 4] [1, 2, 4, 8] [1, 2, 4, 8, 16]
[1, 2, 4, 8, 24] [1, 2, 4, 12] [1, 2, 4, 12, 24] [1, 2, 4, 16]
[1, 2, 4, 24] [1, 2, 6] [1, 2, 6, 12] [1, 2, 6, 12, 24]
[1, 2, 6, 24] [1, 2, 8] [1, 2, 8, 16] [1, 2, 8, 24]
[1, 2, 12] [1, 2, 12, 24] [1, 2, 16] [1, 2, 24]
[1, 3] [1, 3, 6] [1, 3, 6, 12] [1, 3, 6, 12, 24]
[1, 3, 6, 24] [1, 3, 12] [1, 3, 12, 24] [1, 3, 24]
[1, 4] [1, 4, 8] [1, 4, 8, 16] [1, 4, 8, 24]
[1, 4, 12] [1, 4, 12, 24] [1, 4, 16] [1, 4, 24]
[1, 6] [1, 6, 12] [1, 6, 12, 24] [1, 6, 24]
[1, 8] [1, 8, 16] [1, 8, 24] [1, 12]
[1, 12, 24] [1, 16] [1, 24] [1, 48]

48 sequences using second definition:
[2, 24] [2, 2, 12] [2, 2, 2, 6] [2, 2, 2, 2, 3]
[2, 2, 2, 3, 2] [2, 2, 3, 4] [2, 2, 3, 2, 2] [2, 2, 4, 3]
[2, 2, 6, 2] [2, 3, 8] [2, 3, 2, 4] [2, 3, 2, 2, 2]
[2, 3, 4, 2] [2, 4, 6] [2, 4, 2, 3] [2, 4, 3, 2]
[2, 6, 4] [2, 6, 2, 2] [2, 8, 3] [2, 12, 2]
[3, 16] [3, 2, 8] [3, 2, 2, 4] [3, 2, 2, 2, 2]
[3, 2, 4, 2] [3, 4, 4] [3, 4, 2, 2] [3, 8, 2]
[4, 12] [4, 2, 6] [4, 2, 2, 3] [4, 2, 3, 2]
[4, 3, 4] [4, 3, 2, 2] [4, 4, 3] [4, 6, 2]
[6, 8] [6, 2, 4] [6, 2, 2, 2] [6, 4, 2]
[8, 6] [8, 2, 3] [8, 3, 2] [12, 4]
[12, 2, 2] [16, 3] [24, 2] [48]

OEIS A163272:
[0, 1, 48, 1280, 2496, 28672, 29808]
</pre>
</pre>