|
|
|
@ -2,67 +2,22 @@ |
|
|
|
|
#include "utils.h" |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows: |
|
|
|
|
Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
22=4, 23=8, 24=16, 25=32 |
|
|
|
|
32=9, 33=27, 34=81, 35=243 |
|
|
|
|
42=16, 43=64, 44=256, 45=1024 |
|
|
|
|
52=25, 53=125, 54=625, 55=3125 |
|
|
|
|
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms: |
|
|
|
|
|
|
|
|
|
It can be verified that the sum of the numbers on the diagonals is 101. |
|
|
|
|
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 |
|
|
|
|
|
|
|
|
|
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? |
|
|
|
|
How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100? |
|
|
|
|
|
|
|
|
|
https://projecteuler.net/problem=28
|
|
|
|
|
https://projecteuler.net/problem=
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
enum dir{RIGHT,DOWN,LEFT,UP}; |
|
|
|
|
|
|
|
|
|
#define SPIRAL_SIZE 1001 |
|
|
|
|
|
|
|
|
|
int main(int argc,char**argv) { |
|
|
|
|
//Move the number repeatedly in a spiral. Anytime the number in both the X and Y coordinates match, you sum.
|
|
|
|
|
int x=0; |
|
|
|
|
int y=0; |
|
|
|
|
int number=1; |
|
|
|
|
long sum=0; |
|
|
|
|
boolean adjust=false; |
|
|
|
|
int maxCounter=1; |
|
|
|
|
int counter=maxCounter; |
|
|
|
|
enum dir dir=0; |
|
|
|
|
while (x<=SPIRAL_SIZE/2&&y<=SPIRAL_SIZE/2&&x>=-SPIRAL_SIZE/2&&y>=-SPIRAL_SIZE/2) { |
|
|
|
|
if (x==y||-x==y||x==-y||-x==-y) { |
|
|
|
|
sum+=number; |
|
|
|
|
printf("Added %d at (%d,%d). Sum:%ld\n",number,x,y,sum); |
|
|
|
|
} |
|
|
|
|
switch (dir) { |
|
|
|
|
case RIGHT:{ |
|
|
|
|
x++; |
|
|
|
|
}break; |
|
|
|
|
case DOWN:{ |
|
|
|
|
y++; |
|
|
|
|
}break; |
|
|
|
|
case LEFT:{ |
|
|
|
|
x--; |
|
|
|
|
}break; |
|
|
|
|
case UP:{ |
|
|
|
|
y--; |
|
|
|
|
}break; |
|
|
|
|
} |
|
|
|
|
if (--counter==0) { |
|
|
|
|
if (!adjust) { |
|
|
|
|
adjust=true; |
|
|
|
|
} else { |
|
|
|
|
adjust=false; |
|
|
|
|
maxCounter++; |
|
|
|
|
} |
|
|
|
|
dir=(dir+1)%4; |
|
|
|
|
counter=maxCounter; |
|
|
|
|
} |
|
|
|
|
number++; |
|
|
|
|
//printf("(%d,%d)\n",x,y);
|
|
|
|
|
} |
|
|
|
|
printf("\n\nFinal Sum:%ld",sum); |
|
|
|
|
printf("%s",BigPow(3,7).str); |
|
|
|
|
return 0; |
|
|
|
|
} |