tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
21
Task/Perfect-numbers/D/perfect-numbers-1.d
Normal file
21
Task/Perfect-numbers/D/perfect-numbers-1.d
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import std.stdio, std.math, std.range, std.algorithm;
|
||||
|
||||
bool isPerfectNumber(in int n) pure nothrow {
|
||||
if (n < 2)
|
||||
return false;
|
||||
|
||||
int sum = 1;
|
||||
foreach (i; 2 .. cast(int)sqrt(cast(real)n) + 1)
|
||||
if (n % i == 0) {
|
||||
immutable int q = n / i;
|
||||
sum += i;
|
||||
if (q > i)
|
||||
sum += q;
|
||||
}
|
||||
|
||||
return sum == n;
|
||||
}
|
||||
|
||||
void main() {
|
||||
iota(10_000).filter!isPerfectNumber().writeln();
|
||||
}
|
||||
9
Task/Perfect-numbers/D/perfect-numbers-2.d
Normal file
9
Task/Perfect-numbers/D/perfect-numbers-2.d
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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)();
|
||||
}
|
||||
|
||||
void main() {
|
||||
iota(3, 10_000).filter!isPerfect().writeln();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue