diff --git a/archives/27/current b/archives/27/current new file mode 100755 index 0000000..b9a1fc3 Binary files /dev/null and b/archives/27/current differ diff --git a/archives/27/src/main.c b/archives/27/src/main.c new file mode 100644 index 0000000..4e7846a --- /dev/null +++ b/archives/27/src/main.c @@ -0,0 +1,66 @@ +#include +#include "utils.h" + +/* + Euler discovered the remarkable quadratic formula: + + + 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. + + The incredible formula was discovered, which produces 80 primes for the consecutive values . The product of the coefficients, −79 and 1601, is −126479. + + 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 +*/ + +int main(int argc,char**argv) { + FILE*f = fopen("archives/primegenerator/primes","r"); + int startMarker=0; + while (fgetc(f)!='{') { + startMarker++; + } + int primes[2500]; + char c; + int counter=0; + while (counter<2500) { + int digit=0; + while ((c=fgetc(f))!=',') { + digit*=10; + digit+=c-'0'; + } + primes[counter++]=digit; + } + 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; + } + } + 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); + + return 0; +} \ No newline at end of file diff --git a/archives/27/src/utils.c b/archives/27/src/utils.c new file mode 100644 index 0000000..47f5e25 --- /dev/null +++ b/archives/27/src/utils.c @@ -0,0 +1,173 @@ +#include "utils.h" +#include +#include + +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=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=numb2.length) { + for (int offset=0;offsetoffset) { + //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;offsetoffset) { + //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;imaxLength) { + 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); return 0; } \ No newline at end of file