Problem:
A hexagonal tile with number 1 is surrounded by a ring of six hexagonal tiles, starting at "12 o'clock" and numbering the tiles 2 to 7 in an anti-clockwise direction.
New rings are added in the same fashion, with the next rings being numbered 8 to 19, 20 to 37, 38 to 61, and so on. The diagram below shows the first three rings.
By finding the difference between tile n and each its six neighbours we shall define PD(n) to be the number of those differences which are prime.
For example, working clockwise around tile 8 the differences are 12, 29, 11, 6, 1, and 13. So PD(8) = 3.
In the same way, the differences around tile 17 are 1, 17, 16, 1, 11, and 10, hence PD(17) = 2.
It can be shown that the maximum value of PD(n) is 3.
If all of the tiles for which PD(n) = 3 are listed in ascending order to form a sequence, the 10th tile would be 271.
Find the 2000th tile in this sequence.
New rings are added in the same fashion, with the next rings being numbered 8 to 19, 20 to 37, 38 to 61, and so on. The diagram below shows the first three rings.
By finding the difference between tile n and each its six neighbours we shall define PD(n) to be the number of those differences which are prime.
For example, working clockwise around tile 8 the differences are 12, 29, 11, 6, 1, and 13. So PD(8) = 3.
In the same way, the differences around tile 17 are 1, 17, 16, 1, 11, and 10, hence PD(17) = 2.
It can be shown that the maximum value of PD(n) is 3.
If all of the tiles for which PD(n) = 3 are listed in ascending order to form a sequence, the 10th tile would be 271.
Find the 2000th tile in this sequence.
Solution:
669171001
Code:
The solution may include methods that will be found here: Library.java .
public interface EulerSolution{
public String run();
}
/*
* Solution to Project Euler problem 28
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p028 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p028().run());
}
/*
* From the diagram, let's observe the four corners of an n * n square (where n is odd).
* It's not hard to convince yourself that the top right corner always has the value n^2.
* Working counterclockwise (backwards), the top left corner has the value n^2 - (n - 1),
* the bottom left corner has the value n^2 - 2(n - 1), and the bottom right is n^2 - 3(n - 1).
* Putting it all together, this outermost ring contributes 4n^2 - 6(n - 1) to the final sum.
*
* Incidentally, the closed form of this sum is (4m^3 + 3m^2 + 8m - 9) / 6, where m = size.
*/
private static final int SIZE = 1001; // Must be odd
public String run() {
long sum = 1; // Special case for size 1
for (int n = 3; n <= SIZE; n += 2)
sum += 4 * n * n - 6 * (n - 1);
return Long.toString(sum);
}
}
No comments :
Post a Comment