Perfect numbers: Difference between revisions

Updated D entries
(Added COBOL example.)
(Updated D entries)
Line 476:
 
int sum = 1;
foreach (immutable i; 2 .. cast(int)sqrt(cast(real)n) + 1)
if (n % i == 0) {
immutable int q = n / i;
Line 488:
 
void main() {
iota(10_000).iota.filter!isPerfectNumber().writeln();
}</lang>
{{out}}
<pre>[6, 28, 496, 8128]</pre>
With a iota(<code>33_550_337).iota</code> it outputs:
<pre>[6, 28, 496, 8128, 33550336]</pre>
 
===Functional Style===
Same output.
Line 499 ⟶ 500:
 
bool isPerfect(in int n) /*pure nothrow*/ {
return n == iota(1, n - 1).reduce!((s, i) => n % i ? s : s + i)();
}
 
void main() {
iota(3, 10_000).filter!isPerfect().writeln();
}</lang>