This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View 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();
}

View 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();
}