Perfect numbers: Difference between revisions

Content added Content deleted
No edit summary
(Updated to work with Nim 1.4. Changed indentation to conform to guidelines. Corrected the algorithm which computed an incorrect sum for square numbers.)
Line 2,354: Line 2,354:


proc isPerfect(n: int): bool =
proc isPerfect(n: int): bool =
var sum: int = 1
var sum: int = 1
for i in 2 .. <(n.toFloat.sqrt+1).toInt:
for d in 2 .. int(n.toFloat.sqrt):
if n mod i == 0:
if n mod d == 0:
sum += (i + n div i)
inc sum, d
return (n == sum)
let q = n div d
if q != d: inc sum, q
result = n == sum


for i in 2..10_000:
for n in 2..10_000:
if isPerfect(i):
if n.isPerfect:
echo(i)</lang>
echo n</lang>

{{out}}
<pre>6
28
496
8128</pre>


=={{header|Objeck}}==
=={{header|Objeck}}==