Lines 39
Euler28 (8)
##### Permissions
Owner: msgordon
Group Owner: SnortSnort
Viewable by Everyone
Editable by msgordon

# Euler28, C++, Analytical Solution 0

 Language C++
# 's
` 1/********************************************************************* 2   Number spiral diagonals 3    Problem 28 4    Starting with the number 1 and moving to the right in a clockwise 5direction a 5 by 5 spiral is formed as follows: 6 7            21 22 23 24 25 8            20  7  8  9 10 9            19  6  1  2 1110            18  5  4  3 1211            17 16 15 14 131213   It can be verified that the sum of the numbers on the diagonals is 101.1415   What is the sum of the numbers on the diagonals in a 1001 by 100116spiral formed in the same way?17*********************************************************************/1819/*********************************************************************20  We begin by noticing immediately that the top right entry is always21  n^2, where n is the length of the spiral.  The top left entry must22  therefore be (n-1) less than the top right, since there are n numbers23  per side.  This pattern is repeated in a counterclockwise direction24  around the square.  We summarize below:2526  top right:      n^227  top left:       n^2 - (n-1) = n^2- n+128  bottom left:    n^2 -2(n-1) = n^2-2n+229  bottom right:   n^2 -3(n-1) = n^2-3n+33031  Summing up these diagonals yields:  4n^2 - 6n + 632  Therefore, we simply sum all the odd polynomials from n=3->100133*********************************************************************/34#include <iostream>3536using namespace std;3738int main()39{40    const int max_n = 1001;4142    // Start at n = 3, and add in 1 (for n = 1 central 'square')43    int sum = 1;4445    for (int n = 3; n <= max_n; n+=2)46        sum += 4*n*n - 6*n + 6;4748    cout << sum << endl;49}5051// >: 669171001`