- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem028.cpp
More file actions
Latest commit
20 lines (18 loc) · 729 Bytes
/
Copy pathproblem028.cpp
File metadata and controls
20 lines (18 loc) · 729 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
intsumOfMatrixDiagonals(int n)
{
// For any of the concentric squares, the sum of the corners is given by
// n^2 + (n^2 - 3n + 3) + (n^2 - 2n + 2) + (n^2 - n + 1) = 4n^2 - 6n + 6
// where n is the width of the square. The sum of the corners of all the
// squares is the sum from 1 to (n - 1) / 2 of 4(2i + 1)^2 - 6(2i + 1) + 6
// where n is the width of the entire matrix. That sum can be expressed as
// 2k(k(8k + 15) + 13) / 3. Adding 1 for the center of the matrix gives the
// sum of the diagonals.
int k = (n - 1) / 2;
return2 * k * (k * (8 * k + 15) + 13) / 3 + 1;
}
intmain()
{
std::cout << "Answer: " << sumOfMatrixDiagonals(1001) << '\n';
return0;
}