Anonymous user
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() {
}</lang>
{{out}}
<pre>[6, 28, 496, 8128]</pre>
With a
<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
}</lang>
|