Jump to content

Idoneal numbers: Difference between revisions

Created Nim solution.
(Added C)
(Created Nim solution.)
Line 639:
120 130 133 165 168 177 190 210 232 240 253 273 280
312 330 345 357 385 408 462 520 760 840 1320 1365 1848
</pre>
 
=={{header|Nim}}==
To do something different, we use a sieve. This is more efficient as the computations are done only once. But, in this case, this doesn’t change a lot.
<syntaxhighlight lang=Nim>import std/[algorithm, math, strformat]
 
const N = 2000
 
var isIdoneal: array[1..N, bool]
isIdoneal.fill(true)
 
for a in 1..sqrt(N / 3).int:
var p = a * (a + 1)
for b in (a + 1)..(N div (3 * a)):
var n = p + (a + b) * (b + 1)
while n <= N:
isIdoneal[n] = false
inc n, a + b
inc p, a
 
var idx = 0
for n in 1..N:
if isIdoneal[n]:
inc idx
stdout.write &"{n:>4}"
stdout.write if idx mod 13 == 0: '\n' else: ' '
echo()
</syntaxhighlight>
 
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 10 12 13 15
16 18 21 22 24 25 28 30 33 37 40 42 45
48 57 58 60 70 72 78 85 88 93 102 105 112
120 130 133 165 168 177 190 210 232 240 253 273 280
312 330 345 357 385 408 462 520 760 840 1320 1365 1848
</pre>
 
256

edits

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