Erdős-Nicolas numbers: Difference between revisions

julia example
(→‎{{header|Raku}}: Add a Raku example)
(julia example)
Line 37:
714240 113
1571328 115</lang>
 
=={{header!Julia}==
<lang ruby>using Primes
 
function isErdősNicolas_with_k(n)
@assert n > 2
d = [one(n)]
for (p, e) in eachfactor(n)
d = reduce(vcat, [d * p^j for j in 1:e], init=d)
end
sort!(d)
pop!(d)
len = length(d)
(len < 2 || sum(d) == n) && return false, 0
for k in 2:len
sum(@view d[1:k]) == n && return true, k
end
return false, 0
end
 
for n in 3:2_000_000
isEN, k = isErdősNicolas_with_i(n)
isEN && println(lpad(n, 8), " equals the sum of its first $k divisors.")
end
</lang>{{out}}
<pre>
24 equals the sum of its first 6 divisors.
2016 equals the sum of its first 31 divisors.
8190 equals the sum of its first 43 divisors.
42336 equals the sum of its first 66 divisors.
45864 equals the sum of its first 66 divisors.
392448 equals the sum of its first 68 divisors.
714240 equals the sum of its first 113 divisors.
1571328 equals the sum of its first 115 divisors.
</pre>
 
 
=={{header|Phix}}==
4,105

edits