Completed the spiralllll
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
0238e107c6
commit
a851568724
BIN
archives/28/current
Executable file
BIN
archives/28/current
Executable file
Binary file not shown.
68
archives/28/src/main.c
Normal file
68
archives/28/src/main.c
Normal file
@ -0,0 +1,68 @@
|
||||
#include <stdio.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:
|
||||
|
||||
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?
|
||||
|
||||
https://projecteuler.net/problem=28
|
||||
*/
|
||||
|
||||
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);
|
||||
return 0;
|
||||
}
|
173
archives/28/src/utils.c
Normal file
173
archives/28/src/utils.c
Normal file
@ -0,0 +1,173 @@
|
||||
#include "utils.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct String mult(struct String numb1, struct String numb2) {
|
||||
struct String n1 = numb1;
|
||||
struct String n2 = numb2;
|
||||
byte carryover = 0;
|
||||
if (numb2.length>numb1.length) {
|
||||
n1=numb2;
|
||||
n2=numb1;
|
||||
}
|
||||
int addends[n2.length][n1.length+1];
|
||||
for (int i=0;i<n2.length;i++) {
|
||||
for (int j=0;j<n1.length+1;j++) {
|
||||
addends[i][j]=0;
|
||||
}
|
||||
}
|
||||
for (int i=n2.length-1;i>=0;i--) {
|
||||
carryover=0;
|
||||
for (int j=n1.length-1;j>=0;j--) {
|
||||
int mult = (n1.str[j]-'0')*(n2.str[i]-'0')+((carryover!=0)?carryover:0);
|
||||
//printf(" %d/%d\n",mult,carryover);
|
||||
carryover=0;
|
||||
if (mult>=10) {
|
||||
carryover=mult/10;
|
||||
mult=mult%10;
|
||||
}
|
||||
addends[(n2.length-1)-i][j+1]=mult;
|
||||
}
|
||||
if (carryover>0) {
|
||||
addends[(n2.length-1)-i][0]=carryover;
|
||||
}
|
||||
}
|
||||
//printIntDoubleArr(n2.length,n1.length+1,addends);
|
||||
struct String sum = {1,"0"};
|
||||
for (int i=0;i<n2.length;i++) {
|
||||
char val[n1.length+1+i];
|
||||
for (int j=0;j<n1.length+1+i;j++) {
|
||||
val[j]='0';
|
||||
}
|
||||
for (int j=0;j<n1.length+1;j++) {
|
||||
val[j]=addends[i][j]+'0';
|
||||
}
|
||||
sum=add((struct String){n1.length+1+i,val},sum);
|
||||
//printf("%s\n",sum.str);
|
||||
}
|
||||
if (sum.str[0]=='0') {
|
||||
char*newStr=malloc(sum.length-1);
|
||||
for (int i=1;i<sum.length;i++) {
|
||||
newStr[i-1]=sum.str[i];
|
||||
}
|
||||
free(sum.str);
|
||||
sum=(struct String){sum.length-1,newStr};
|
||||
}
|
||||
//printf("%s",sum.str);
|
||||
return sum;
|
||||
}
|
||||
|
||||
struct String add(struct String numb1, struct String numb2){
|
||||
//printf("%s %s\n",numb1.str,numb2.str);
|
||||
byte carryover=0;
|
||||
int digitCount=0;
|
||||
char*str = malloc(1);
|
||||
//str[0]='\0';
|
||||
if (numb1.length>=numb2.length) {
|
||||
for (int offset=0;offset<numb1.length;offset++) {
|
||||
str = realloc(str,++digitCount);
|
||||
//printf("Digit count is now %d\n",digitCount);
|
||||
if (numb2.length>offset) {
|
||||
//printf("%c %c\n",numb1.str[numb1.length-offset-1],numb2.str[numb2.length-offset-1]);
|
||||
int sum=((numb1.str[numb1.length-offset-1]-'0')+(numb2.str[numb2.length-offset-1]-'0'))+((carryover>0)?carryover--:0);
|
||||
if (sum>=10) {
|
||||
carryover=1;
|
||||
sum-=10;
|
||||
}
|
||||
str[offset]=sum+'0';
|
||||
} else {
|
||||
str[offset]=numb1.str[numb1.length-offset-1]+((carryover>0)?carryover--:0);
|
||||
}
|
||||
//str[offset+1]='\0';
|
||||
}
|
||||
} else {
|
||||
for (int offset=0;offset<numb2.length;offset++) {
|
||||
str = realloc(str,++digitCount);
|
||||
//printf("Digit count is now %d\n",digitCount);
|
||||
if (numb1.length>offset) {
|
||||
//printf("%c %c\n",numb1.str[numb1.length-offset-1],numb2.str[numb2.length-offset-1]);
|
||||
int sum=((numb1.str[numb1.length-offset-1]-'0')+(numb2.str[numb2.length-offset-1]-'0'))+((carryover>0)?carryover--:0);
|
||||
if (sum>=10) {
|
||||
carryover=1;
|
||||
sum-=10;
|
||||
}
|
||||
str[offset]=sum+'0';
|
||||
} else {
|
||||
str[offset]=numb2.str[numb2.length-offset-1]+((carryover>0)?carryover--:0);
|
||||
}
|
||||
//str[offset+1]='\0';
|
||||
}
|
||||
}
|
||||
if (carryover>0) {
|
||||
str = realloc(str,++digitCount);
|
||||
str[digitCount-1]='1';
|
||||
//str[digitCount]='\0';
|
||||
}
|
||||
for (int i=0;i<digitCount/2;i++) {
|
||||
char c = str[i];
|
||||
char c2 = str[digitCount-i-1];
|
||||
str[digitCount-i-1]=c;
|
||||
str[i]=c2;
|
||||
}
|
||||
str = realloc(str,digitCount+1);
|
||||
str[digitCount]='\0';
|
||||
struct String newStr = {digitCount,str};
|
||||
return newStr;
|
||||
}
|
||||
|
||||
void printLongDoubleArr(int a,int b,long doubleArr[a][b]) {
|
||||
for (int i=0;i<a;i++) {
|
||||
for (int j=0;j<b;j++) {
|
||||
printf("%ld\t",doubleArr[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void printIntDoubleArr(int a,int b,int doubleArr[a][b]) {
|
||||
for (int i=0;i<a;i++) {
|
||||
for (int j=0;j<b;j++) {
|
||||
printf("%d\t",doubleArr[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
struct String createBigNumber(char*numb) {
|
||||
int marker=0;
|
||||
while (numb[marker++]!='\0');
|
||||
return (struct String){marker-1,numb};
|
||||
}
|
||||
|
||||
int*getFactors(int numb) {
|
||||
int*factorList=malloc(sizeof(int)*1);
|
||||
int factorListSize=2;
|
||||
factorList[0]=1;
|
||||
factorList[1]=numb;
|
||||
int max=numb;
|
||||
for (int i=2;i<max;i++) {
|
||||
if (numb%i==0) {
|
||||
factorList=realloc(factorList,sizeof(int)*++factorListSize);
|
||||
factorList[factorListSize-1]=i;
|
||||
if (numb/i!=i) {
|
||||
factorList=realloc(factorList,sizeof(int)*++factorListSize);
|
||||
factorList[factorListSize-1]=numb/i;
|
||||
max=numb/i;
|
||||
}
|
||||
}
|
||||
}
|
||||
factorList=realloc(factorList,sizeof(int)*++factorListSize);
|
||||
factorList[factorListSize-1]=0;
|
||||
return factorList;
|
||||
}
|
||||
|
||||
boolean isFactor(int*factorList,int numb) {
|
||||
int counter=0;
|
||||
while (factorList[counter]!=0) {
|
||||
if (numb==factorList[counter]) {
|
||||
return true;
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
return false;
|
||||
}
|
16
archives/28/src/utils.h
Normal file
16
archives/28/src/utils.h
Normal file
@ -0,0 +1,16 @@
|
||||
#define true 1
|
||||
#define false 0
|
||||
#define boolean char
|
||||
#define byte char
|
||||
struct String{
|
||||
int length;
|
||||
char*str;
|
||||
};
|
||||
struct String add(struct String numb1, struct String numb2);
|
||||
struct String mult(struct String numb1, struct String numb2);
|
||||
void printLongDoubleArr(int a,int b,long doubleArr[a][b]);
|
||||
void printIntDoubleArr(int a,int b,int doubleArr[a][b]);
|
||||
struct String createBigNumber(char*numb);
|
||||
#define BigNumber(X) createBigNumber(#X)
|
||||
boolean isFactor(int*factorList,int numb);
|
||||
int*getFactors(int numb);
|
98
src/main.c
98
src/main.c
@ -2,65 +2,67 @@
|
||||
#include "utils.h"
|
||||
|
||||
/*
|
||||
Euler discovered the remarkable quadratic formula:
|
||||
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 turns out that the formula will produce 40 primes for the consecutive integer values . However, when is divisible by 41, and certainly when is clearly divisible by 41.
|
||||
It can be verified that the sum of the numbers on the diagonals is 101.
|
||||
|
||||
The incredible formula was discovered, which produces 80 primes for the consecutive values . The product of the coefficients, −79 and 1601, is −126479.
|
||||
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
|
||||
|
||||
Considering quadratics of the form:
|
||||
|
||||
, where and
|
||||
|
||||
where is the modulus/absolute value of
|
||||
e.g. and
|
||||
Find the product of the coefficients, and , for the quadratic expression that produces the maximum number of primes for consecutive values of , starting with .
|
||||
|
||||
https://projecteuler.net/problem=27
|
||||
https://projecteuler.net/problem=28
|
||||
*/
|
||||
|
||||
enum dir{RIGHT,DOWN,LEFT,UP};
|
||||
|
||||
#define SPIRAL_SIZE 1001
|
||||
|
||||
int main(int argc,char**argv) {
|
||||
FILE*f = fopen("archives/primegenerator/primes","r");
|
||||
int startMarker=0;
|
||||
while (fgetc(f)!='{') {
|
||||
startMarker++;
|
||||
//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);
|
||||
}
|
||||
int primes[2500];
|
||||
char c;
|
||||
int counter=0;
|
||||
while (counter<2500) {
|
||||
int digit=0;
|
||||
while ((c=fgetc(f))!=',') {
|
||||
digit*=10;
|
||||
digit+=c-'0';
|
||||
switch (dir) {
|
||||
case RIGHT:{
|
||||
x++;
|
||||
}break;
|
||||
case DOWN:{
|
||||
y++;
|
||||
}break;
|
||||
case LEFT:{
|
||||
x--;
|
||||
}break;
|
||||
case UP:{
|
||||
y--;
|
||||
}break;
|
||||
}
|
||||
primes[counter++]=digit;
|
||||
if (--counter==0) {
|
||||
if (!adjust) {
|
||||
adjust=true;
|
||||
} else {
|
||||
adjust=false;
|
||||
maxCounter++;
|
||||
}
|
||||
int n=0;
|
||||
|
||||
int maxLength=0;
|
||||
int maxA=0;
|
||||
int maxB=0;
|
||||
|
||||
for (int a=-999;a<1000;a++) {
|
||||
for (int b=-999;b<=1000;b++) {
|
||||
for (int p=0;p<2500;p++) {
|
||||
if (n*n+a*n+b==primes[p]) {
|
||||
n++;
|
||||
p=0;
|
||||
dir=(dir+1)%4;
|
||||
counter=maxCounter;
|
||||
}
|
||||
number++;
|
||||
//printf("(%d,%d)\n",x,y);
|
||||
}
|
||||
if (n>maxLength) {
|
||||
maxLength=n;
|
||||
maxA=a;
|
||||
maxB=b;
|
||||
printf("New max of %d found with n^2+%dn+%d\n",maxLength,maxA,maxB);
|
||||
}
|
||||
n=0;
|
||||
}
|
||||
}
|
||||
printf("\n\nProduct of maxes (%d*%d)=%ld\n",maxA,maxB,(long)maxA*maxB);
|
||||
|
||||
printf("\n\nFinal Sum:%ld",sum);
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user