Skip to content

Instantly share code, notes, and snippets.

@logalleon
Last active August 29, 2015 14:24
Show Gist options
  • Save logalleon/26ce127648ecc8efc131 to your computer and use it in GitHub Desktop.
Save logalleon/26ce127648ecc8efc131 to your computer and use it in GitHub Desktop.
Project Euler 28
#include <iostream>
using namespace::std;
// Calculates the sum of the diagonals of number spiral size s x s.
int spiralDiagonalSum(int s) {
// The size of the spiral
int start = s*s;
// Starting array offset
int offset = s-1;
int answer = 0;
while (offset > 0) {
// Sum each of 4 "corners"
for (int i = 0; i < 4; i++){
answer += start;
// Move to the next corner
start -= offset;
}
// Move one row in / one column in
offset -= 2;
}
// Sum of all corners + 1 (the center of the spiral)
return answer + 1;
}
int main(void) {
// SHOW ME THAT SUM YEAAAAAAAH
cout << spiralDiagonalSum(1001);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment