Multiplicatively perfect numbers: Difference between revisions
→{{header|Wren}}: Optimized to enable 5 million to be reached in a reasonable time.
m (→{{header|Phix}}: extended limit to match Raku) |
(→{{header|Wren}}: Optimized to enable 5 million to be reached in a reasonable time.) |
||
Line 800:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
This includes '1' as an MPN. Not very quick at around 112 seconds but reasonable for Wren.
<syntaxhighlight lang="ecmascript">import "./math" for Int, Nums
import "./fmt" for Fmt
// library method customized for this task
var divisors = Fn.new { |n|
var divisors = []
var i = 1
var k = (n%2 == 0) ? 1 : 2
while (i <= n.sqrt) {
if (i > 1 && n%i == 0) { // exclude 1 and n
divisors.add(i)
if (divisors.count > 2) break // not eligible if has > 2 divisors
var j = (n/i).floor
if (j != i) divisors.add(j)
}
i = i + k
}
return divisors
}
var limit = 500
var count = 0
var i =
System.print("Multiplicatively perfect numbers under %(limit):")
while (true) {
var pd = (i != 1) ?
if (pd.count
count = count + 1
if (i < 500) {
var pds =
Fmt.write("$3d = $s ", i, pds)
if (count % 5 == 0) System.print()
Line 823 ⟶ 840:
var cubes = Int.primeCount((limit - 1).cbrt.floor)
var count2 = count + squares - cubes - 1
Fmt.print("Counts under $,
if (limit ==
limit = limit * 10
}
Line 864 ⟶ 881:
482 = 2 x 241 485 = 5 x 97 489 = 3 x 163 493 = 17 x 29 497 = 7 x 71
Counts under 500: MPNs = 150 Semi-primes = 153
Counts under 5,000: MPNs = 1,354 Semi-primes = 1,365
Counts under 50,000: MPNs = 12,074 Semi-primes = 12,110
Counts under 500,000: MPNs = 108,223 Semi-primes = 108,326
Counts under 5,000,000: MPNs = 978,983 Semi-primes = 979,274
</pre>
|