-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectEuler028.cpp
More file actions
40 lines (30 loc) · 829 Bytes
/
Copy pathprojectEuler028.cpp
File metadata and controls
40 lines (30 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* Problem 28
Number Spiral Diagonals
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?
*/
#include <iomanip>
#include <iostream>
using namespace std;
int main () {
double sumDiagonals = 1;
int lastTerm = 1;
for (int i = 1; i <= 500; i++) {
for (int j = 1; j <= 4; j++) {
sumDiagonals += double (2*(i * j) + lastTerm);
if (j == 4) {
lastTerm = 2*(i * j) + lastTerm;
}
}
}
cout << setprecision (0) << fixed;
cout << sumDiagonals << endl;
}