Project Euler > Problem 28 > Number spiral diagonals (Java Solution)

Problem:

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13

It can be verified that the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?


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

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact