I have been working on Project Euler problem 211 for quite some time, and I am stuck. I'm not looking for an answer, I'm simply looking for some guidance. I've written and tested the following code, which literally has run for days on my machine without completing.
System.out.println(IntStream.range(1, LIMIT).filter(input -> isPerfectSquare(phi(input))).sum());
static long phi(final int n) {
return streamedDivisors(n).map(x -> x * x).sum();
}
static LongStream streamedDivisors(final int n) {
return LongStream.range(1, n + 1).parallel().filter(input -> input == 1 || n % input == 0);
}
static boolean isPerfectSquare(final long n)
{
if (n < 0)
return false;
long tst = (long)(Math.sqrt(n) + 0.5);
return tst*tst == n;
}
and I've concluded there must be a shortcut that I'm not seeing. Can someone point me in the right direction?
What I'm trying to pick up is the reasoning behind whatever shortcut one could take for not calculating the divisors for each of the 64 million numbers, which computationally is the most expensive part of this exercise (and what I assume is the key!)