Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 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 @@
|
|||
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() {
|
||||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
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, 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue