Perfect numbers: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) 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 |
|||
for d in 2 .. int(n.toFloat.sqrt): |
|||
if n mod d == 0: |
|||
inc sum, d |
|||
let q = n div d |
|||
if q != d: inc sum, q |
|||
result = n == sum |
|||
for |
for n in 2..10_000: |
||
if n.isPerfect: |
|||
echo n</lang> |
|||
{{out}} |
|||
<pre>6 |
|||
28 |
|||
496 |
|||
8128</pre> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |