Commit 220032e3 by Andrew Dahl

Solved Problem 28

parent 2df37610
Showing with 37 additions and 0 deletions
Question:
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?
Answer: 669171001
#include <iostream>
using namespace std;
const int SQUARE_SIZE = 1001;
int main() {
int sum = 1;
int side = 2;
int corner = 1;
for(int i = 0; i < SQUARE_SIZE/2; i++) {
for(int j = 0; j < 4; j++) {
corner += side;
sum += corner;
}
side += 2;
}
cout << sum << endl;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment