Jump to content

Perfect numbers: Difference between revisions

Updated both D entries
(Updated both D entries)
Line 366:
 
void main() {
writelniota(10_000).filter!isPerfectNumber(iota(33_550_337)).writeln();
}</lang>
{{out}}
Output:
<pre>[6, 28, 496, 8128]</pre>
With a iota(33_550_337) it outputs:
<pre>[6, 28, 496, 8128, 33550336]</pre>
===Functional Style===
Same output.
<lang d>import std.stdio, std.algorithm, std.range;
 
bool isPerfect(in int n) /*pure nothrow*/ {
return n == iota(1, n - 1).reduce!((s, i) => n % i ? s : s + i)(iota(1, n-1));
}
 
void main() {
writeln(filter!isPerfect(iota(3, 10_000).filter!isPerfect().writeln();
}</lang>
Output:
<pre>[6, 28, 496, 8128]</pre>
 
=={{header|E}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.