BigPow() function

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2, Sig, Sigo 3 years ago
parent a851568724
commit 2b11336692
  1. BIN
      current
  2. 65
      src/main.c
  3. 17
      src/utils.c
  4. 1
      src/utils.h

Binary file not shown.

@ -2,67 +2,22 @@
#include "utils.h" #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 22=4, 23=8, 24=16, 25=32
20 7 8 9 10 32=9, 33=27, 34=81, 35=243
19 6 1 2 11 42=16, 43=64, 44=256, 45=1024
18 5 4 3 12 52=25, 53=125, 54=625, 55=3125
17 16 15 14 13 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) { 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. printf("%s",BigPow(3,7).str);
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);
return 0; return 0;
} }

@ -170,4 +170,21 @@ boolean isFactor(int*factorList,int numb) {
counter++; counter++;
} }
return false; return false;
}
struct String BigPow(int numb1, int numb2) {
struct String sum = BigNumber(1);
char*newStr = malloc(0);
int strLength=0;
while (numb1>0) {
newStr=realloc(newStr,++strLength);
newStr[strLength-1]=(numb1%10)+'0';
numb1/=10;
}
struct String bigNumb2 = (struct String){strLength,newStr};
for (int i=0;i<numb2;i++) {
sum=mult(sum,bigNumb2);
}
free(newStr);
return sum;
} }

@ -8,6 +8,7 @@ struct String{
}; };
struct String add(struct String numb1, struct String numb2); struct String add(struct String numb1, struct String numb2);
struct String mult(struct String numb1, struct String numb2); struct String mult(struct String numb1, struct String numb2);
struct String BigPow(int numb1,int numb2);
void printLongDoubleArr(int a,int b,long doubleArr[a][b]); void printLongDoubleArr(int a,int b,long doubleArr[a][b]);
void printIntDoubleArr(int a,int b,int doubleArr[a][b]); void printIntDoubleArr(int a,int b,int doubleArr[a][b]);
struct String createBigNumber(char*numb); struct String createBigNumber(char*numb);

Loading…
Cancel
Save