Puzzle (8-14) (answer)
bearophile
bearophileHUGS at lycos.com
Thu Aug 14 12:49:42 PDT 2008
Steven Schveighoffer:
> attempt 2 (little bit of cheating, because I know the answer fits within an
> int):
...
I have modified your code a little, using a poor's man set to speed up the code:
import std.stdio: putr = writefln;
import std.math: sqrt;
void main() {
int[][int] pairs;
int[int] squares;
for (int i = 1; i < short.max; i++)
squares[i * i] = 0;
for (int x = 3; ; x++) {
for (int i = 1; i * i < x; i++) {
int y = x - (i * i);
if ((x + y) in squares) {
auto arr = y in pairs;
if (arr !is null) {
foreach (z; *arr) {
if ((x - z) in squares && (x + z) in squares) {
putr(x, " ", y, " ", z);
return;
}
}
}
arr = x in pairs;
if (arr is null) {
pairs[x] = new int[0];
arr = x in pairs;
}
*arr ~= y;
}
}
}
}
On my 2 GHz PC it takes 6.97 s to run. I have another trick to try, we'll see if it works...
Later,
bearophile
More information about the Digitalmars-d-learn
mailing list