forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p028.mat.txt
22 lines (19 loc) · 895 Bytes
/
p028.mat.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(*
* Solution to Project Euler problem 28
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*)
(* Use this in Wolfram Mathematica *)
(*
* 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.
*)
size = 1001; (* Must be odd *)
Sum[Block[{n = 2k + 1}, 4 * n^2 - 6 * (n - 1)], {k, (size - 1) / 2}] + 1