parent
3ea3137f8a
commit
eb9733c9d7
Binary file not shown.
@ -0,0 +1,211 @@ |
||||
#include <stdio.h> |
||||
#include "utils.h" |
||||
#include <stdlib.h> |
||||
|
||||
/*
|
||||
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given: |
||||
|
||||
1/2 = 0.5 |
||||
1/3 = 0.(3) |
||||
1/4 = 0.25 |
||||
1/5 = 0.2 |
||||
1/6 = 0.1(6) |
||||
1/7 = 0.(142857) |
||||
1/8 = 0.125 |
||||
1/9 = 0.(1) |
||||
1/10 = 0.1 |
||||
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle. |
||||
|
||||
Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. |
||||
|
||||
https://projecteuler.net/problem=26
|
||||
*/ |
||||
|
||||
boolean isPrime(int*primeList,int primeListSize,int numb) { |
||||
for (int i=0;i<primeListSize;i++) { |
||||
if (numb==primeList[i]) { |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
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; |
||||
} |
||||
|
||||
const int TARGET_REPEATS_REQUIRED=100000; |
||||
|
||||
int main(int argc,char**argv) { |
||||
int divider=0; |
||||
int divisor=2; |
||||
int sequence[TARGET_REPEATS_REQUIRED]; //Let's assume for the sake of sanity that 100000 repeating digits is enough. I sure hope so.
|
||||
int sequenceLength=0; |
||||
int sequenceRepeat=0; |
||||
int sequenceMarker=-1; |
||||
int longestCycleLength=0; |
||||
int longestCycleDivisor=0; |
||||
int longestSequence[TARGET_REPEATS_REQUIRED/10]; |
||||
int*primeList=malloc(sizeof(int)*0); |
||||
int primeListSize=0; |
||||
|
||||
FILE*f = fopen("archives/primegenerator/primes","r"); |
||||
while (fgetc(f)!='{'); |
||||
while (true) { |
||||
char c; |
||||
int digit=0; |
||||
while ((c=fgetc(f))!=',') { |
||||
digit*=10; |
||||
digit+=c-'0'; |
||||
}
|
||||
if (digit>=1000) { |
||||
break; |
||||
} else { |
||||
primeList=realloc(primeList,sizeof(int)*++primeListSize); |
||||
primeList[primeListSize-1]=digit; |
||||
} |
||||
} |
||||
|
||||
while (divisor<1000) { |
||||
divider=10; //We always start with 10.
|
||||
sequenceLength=0; |
||||
sequenceRepeat=0; |
||||
for (int i=0;i<sequenceLength;i++) { |
||||
sequence[i]=0; |
||||
} |
||||
sequenceMarker=-1; |
||||
while (sequenceLength<TARGET_REPEATS_REQUIRED) { |
||||
int result=divider/divisor; |
||||
//printf("%d/%d = %d\n",divider,divisor,result);
|
||||
divider=divider%divisor; |
||||
sequence[sequenceLength++]=result; |
||||
divider*=10; |
||||
if (divider==0) { |
||||
//We're solved.
|
||||
break; |
||||
} |
||||
} |
||||
/*printf("%d:",divisor);
|
||||
for (int i=0;i<sequenceLength;i++) { |
||||
printf("%d",sequence[i]); |
||||
} |
||||
printf("\n");*/ |
||||
//We need to look at all combinations of possible repeating sequences.
|
||||
//Starting from iteration loop of length 1, and an offset incrementing by 1, see if anything remains the same.
|
||||
//Then do this for iteration loop of length 2, offset incrementing by 1, etc.
|
||||
//Find it repeating at least 20 times to assume it repeats infinitely.
|
||||
boolean isP = isPrime(primeList,primeListSize,divisor); |
||||
int*factors = getFactors(divisor-1); |
||||
int counter=0; |
||||
/*if (isP) {
|
||||
printf("Possible length factors of (%d-1):",divisor); |
||||
while (factors[counter]!=0) { |
||||
printf("%d,",factors[counter]); |
||||
counter++; |
||||
} |
||||
printf("\n"); |
||||
}*/ |
||||
boolean matched=false; |
||||
if (sequenceLength==TARGET_REPEATS_REQUIRED) { |
||||
for (int checkLength=0;checkLength<TARGET_REPEATS_REQUIRED/10;checkLength++) { |
||||
if (isP&&!isFactor(factors,checkLength+1)) {continue;} |
||||
for (int offset=0;offset<sequenceLength+1-checkLength;offset++) { |
||||
int sequenceStore[checkLength+1]; |
||||
int requiredCheckAmt=TARGET_REPEATS_REQUIRED/10-2; |
||||
for (int i=0;i<checkLength+1;i++) { |
||||
sequenceStore[i]=sequence[i+offset]; |
||||
/*printf("%d:",divisor);
|
||||
for (int i=0;i<checkLength+1;i++) { |
||||
printf("%d",sequenceStore[i]); |
||||
} |
||||
0010030090270812437311935807422266800401203610832497492477432296890672016048144433299899699097291875626880641925777331995987963891675025075225677031093279839518555667 |
||||
0010030090270812437311935807422266800401203610832497492477432296890672016048144433299899699097291875626880641925777331995987963891675025075225677031093279839518555667 |
||||
printf("\n");*/ |
||||
} |
||||
//Now if this repeating sequence can be found requiredCheckAmt times, we're golden.
|
||||
boolean allMatching=true; |
||||
int currentOffset=0; |
||||
int found=0; |
||||
while (requiredCheckAmt>0&&found<20) { |
||||
for (int i=0;i<checkLength+1;i++) { |
||||
//printf(" Compare %d to %d\n",sequence[offset+currentOffset+checkLength+1+i],sequenceStore[i]);
|
||||
if (sequence[offset+currentOffset+checkLength+1+i]!=sequenceStore[i]) { |
||||
allMatching=false; |
||||
goto check; |
||||
} |
||||
} |
||||
requiredCheckAmt--; |
||||
currentOffset+=checkLength+1; |
||||
found++; |
||||
//printf("Found a repeat.");
|
||||
} |
||||
check: |
||||
if (allMatching) { |
||||
printf("Longest repeating sequence for %d is of length %d: ",divisor,checkLength+1); |
||||
for (int i=0;i<checkLength+1;i++) { |
||||
printf("%d",sequenceStore[i]); |
||||
} |
||||
printf("\n"); |
||||
if (longestCycleLength<checkLength+1) { |
||||
longestCycleLength=checkLength+1; |
||||
longestCycleDivisor=divisor; |
||||
for (int i=0;i<longestCycleLength;i++) { |
||||
longestSequence[i]=sequenceStore[i]; |
||||
} |
||||
} |
||||
matched=true; |
||||
goto next; |
||||
} |
||||
} |
||||
} |
||||
} else { |
||||
matched=true; |
||||
printf("Longest repeating sequence for %d is of length 0.\n",divisor); |
||||
} |
||||
next: |
||||
free(factors); |
||||
if (!matched) { |
||||
printf("Not enough data to calculate longest repeating sequence for %d!\nQuitting...\n",divisor); |
||||
return 1; |
||||
} |
||||
divisor++; |
||||
} |
||||
|
||||
printf("\n\nThe largest repeating sequence length is from %d with %d in length.",longestCycleDivisor,longestCycleLength); |
||||
printf("\n\tSequence: "); |
||||
for (int i=0;i<longestCycleLength;i++) { |
||||
printf("%d",longestSequence[i]); |
||||
} |
||||
printf("\n"); |
||||
|
||||
return 0; |
||||
} |
@ -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; |
||||
} |
@ -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); |
Loading…
Reference in new issue