Jump to content

Numbers which are the cube roots of the product of their proper divisors: Difference between revisions

Created Nim solution.
No edit summary
(Created Nim solution.)
Line 1,017:
5,000th: 23118
50,000th: 223735</pre>
 
=={{header|Nim}}==
We use an iterator rather than storing the divisors in a sequence. This prevent to optimize by checking the number of divisors, but the program is actually more efficient this way as there is no allocations. It runs in about 400 ms on an Intel Core i5-8250U CPU @ 1.60GHz × 4.
<syntaxhighlight lang="Nim">import std/strformat
 
iterator properDivisors(n: Positive): Positive =
## Yield the proper divisors, except 1.
var d = 2
while d * d <= n:
if n mod d == 0:
yield d
let q = n div d
if q != d: yield q
inc d
 
iterator a111398(): (int, int) =
## Yield the successive elements of the OEIS A111398 sequence.
yield (1, 1)
var idx = 1
var n = 1
while true:
inc n
var p = 1
block Check:
let n3 = n * n * n
for d in properDivisors(n):
p *= d
if p > n3: break Check # Two large: try next value.
if n3 == p:
inc idx
yield (idx, n)
 
echo "First 50 numbers which are the cube roots of the products of their proper divisors:"
for (i, n) in a111398():
if i <= 50:
stdout.write &"{n:>3}"
stdout.write if i mod 10 == 0: '\n' else: ' '
stdout.flushFile
elif i in [500, 5000, 50000]:
echo &"{i:>5}th: {n:>6}"
if i == 50000: break
</syntaxhighlight>
 
{{out}}
<pre>First 50 numbers which are the cube roots of the products of their proper divisors:
1 24 30 40 42 54 56 66 70 78
88 102 104 105 110 114 128 130 135 136
138 152 154 165 170 174 182 184 186 189
190 195 222 230 231 232 238 246 248 250
255 258 266 273 282 285 286 290 296 297
500th: 2526
5000th: 23118
50000th: 223735
</pre>
 
=={{header|Pascal}}==
256

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.