Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
12
Task/Perfect-numbers/D/perfect-numbers-1.d
Normal file
12
Task/Perfect-numbers/D/perfect-numbers-1.d
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
bool isPerfectNumber1(in uint n) pure nothrow
|
||||
in {
|
||||
assert(n > 0);
|
||||
} body {
|
||||
return n == iota(1, n - 1).filter!(i => n % i == 0).sum;
|
||||
}
|
||||
|
||||
void main() {
|
||||
iota(1, 10_000).filter!isPerfectNumber1.writeln;
|
||||
}
|
||||
21
Task/Perfect-numbers/D/perfect-numbers-2.d
Normal file
21
Task/Perfect-numbers/D/perfect-numbers-2.d
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import std.stdio, std.math, std.range, std.algorithm;
|
||||
|
||||
bool isPerfectNumber2(in int n) pure nothrow {
|
||||
if (n < 2)
|
||||
return false;
|
||||
|
||||
int total = 1;
|
||||
foreach (immutable i; 2 .. cast(int)real(n).sqrt + 1)
|
||||
if (n % i == 0) {
|
||||
immutable int q = n / i;
|
||||
total += i;
|
||||
if (q > i)
|
||||
total += q;
|
||||
}
|
||||
|
||||
return total == n;
|
||||
}
|
||||
|
||||
void main() {
|
||||
10_000.iota.filter!isPerfectNumber2.writeln;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue