A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
23
Task/Happy-numbers/D/happy-numbers-1.d
Normal file
23
Task/Happy-numbers/D/happy-numbers-1.d
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
bool isHappy(int n) pure nothrow {
|
||||
int[int] past;
|
||||
|
||||
while (true) {
|
||||
int total = 0;
|
||||
while (n > 0) {
|
||||
total += (n % 10) ^^ 2;
|
||||
n /= 10;
|
||||
}
|
||||
if (total == 1)
|
||||
return true;
|
||||
if (total in past)
|
||||
return false;
|
||||
n = total;
|
||||
past[total] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
int.max.iota().filter!isHappy().take(8).writeln();
|
||||
}
|
||||
19
Task/Happy-numbers/D/happy-numbers-2.d
Normal file
19
Task/Happy-numbers/D/happy-numbers-2.d
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import std.stdio, std.algorithm, std.range, std.conv;
|
||||
|
||||
bool isHappy(int n) /*pure nothrow*/ {
|
||||
int[int] seen;
|
||||
|
||||
while (true) {
|
||||
const t = n.text().map!q{(a - '0') ^^ 2}().reduce!q{a + b}();
|
||||
if (t == 1)
|
||||
return true;
|
||||
if (t in seen)
|
||||
return false;
|
||||
n = t;
|
||||
seen[t] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
int.max.iota().filter!isHappy().take(8).writeln();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue