RosettaCodeData/Task/Happy-numbers/D/happy-numbers-2.d
2023-07-01 13:44:08 -04:00

19 lines
416 B
D

import std.stdio, std.algorithm, std.range, std.conv, std.string;
bool isHappy(int n) pure nothrow {
int[int] seen;
while (true) {
immutable t = n.text.representation.map!q{(a - '0') ^^ 2}.sum;
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;
}