Fix the bugs regarding extra characters appearing with big number functions

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
sigonasr2, Sig, Sigo 2022-07-19 14:19:14 +00:00 committed by GitHub
parent ef69569a43
commit e189446fe0
5 changed files with 42 additions and 37 deletions

BIN
current

Binary file not shown.

1
p022_names.txt Normal file

File diff suppressed because one or more lines are too long

View File

@ -3,49 +3,39 @@
#include <stdlib.h> #include <stdlib.h>
/* /*
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
If d(a) = b and d(b) = a, where a b, then a and b are an amicable pair and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220. For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
Evaluate the sum of all the amicable numbers under 10000. What is the total of all the name scores in the file?
https://projecteuler.net/problem=21 https://projecteuler.net/problem=22
*/ */
int main(int argc,char**argv) { int main(int argc,char**argv) {
int counter=1; FILE*file = fopen("p022_names.txt","r");
long totalSum=0; printf("%s\n",add(BigNumber(38195431534281091050778),BigNumber(381918429378)).str);
while (counter<10000) { /*int pointer=0;
//printf("Factor sum of %d: 1,",counter); int nameCount=0;
int factorSum=1; char**names = malloc(sizeof(char*));
int max=counter/2; while (!feof(file)) {
for (int i=2;i<max;i++) { if (fgetc(file)=='"') {
if (counter%i==0) { //Start reading the name.
factorSum+=i; char*newName=malloc(1);
if (i!=(counter/i)) { char c=' ';
factorSum+=counter/i; int length=0;
while ((c=fgetc(file))!='"') {
newName=realloc(newName,++length);
newName[length-2]=c;
newName[length-1]='\0';
} }
max=counter/i; nameCount++;
//printf("%d,%d,",i,counter/i); names=realloc(names,sizeof(char*)*nameCount);
names[nameCount-1]=newName;
} }
} }
//printf("\n"); for (int i=0;i<nameCount;i++) {
int factorSum2=1; printf("%s\n",names[i]);
max=factorSum/2; }*/
for (int i=2;i<max;i++) {
if (factorSum%i==0) {
factorSum2+=i;
factorSum2+=factorSum/i;
max=factorSum/i;
}
}
if (factorSum!=factorSum2&&counter==factorSum2) {
printf("%d\n",counter);
totalSum+=counter;
}
counter++;
}
printf("\n\nAmicable Number sum: %ld",totalSum);
return 0; return 0;
} }

View File

@ -61,7 +61,8 @@ struct String add(struct String numb1, struct String numb2){
//printf("%s %s\n",numb1.str,numb2.str); //printf("%s %s\n",numb1.str,numb2.str);
byte carryover=0; byte carryover=0;
int digitCount=0; int digitCount=0;
char*str = malloc(0); char*str = malloc(1);
//str[0]='\0';
if (numb1.length>=numb2.length) { if (numb1.length>=numb2.length) {
for (int offset=0;offset<numb1.length;offset++) { for (int offset=0;offset<numb1.length;offset++) {
str = realloc(str,++digitCount); str = realloc(str,++digitCount);
@ -77,6 +78,7 @@ struct String add(struct String numb1, struct String numb2){
} else { } else {
str[offset]=numb1.str[numb1.length-offset-1]+((carryover>0)?carryover--:0); str[offset]=numb1.str[numb1.length-offset-1]+((carryover>0)?carryover--:0);
} }
//str[offset+1]='\0';
} }
} else { } else {
for (int offset=0;offset<numb2.length;offset++) { for (int offset=0;offset<numb2.length;offset++) {
@ -93,11 +95,13 @@ struct String add(struct String numb1, struct String numb2){
} else { } else {
str[offset]=numb2.str[numb2.length-offset-1]+((carryover>0)?carryover--:0); str[offset]=numb2.str[numb2.length-offset-1]+((carryover>0)?carryover--:0);
} }
//str[offset+1]='\0';
} }
} }
if (carryover>0) { if (carryover>0) {
str = realloc(str,++digitCount); str = realloc(str,++digitCount);
str[digitCount-1]='1'; str[digitCount-1]='1';
//str[digitCount]='\0';
} }
for (int i=0;i<digitCount/2;i++) { for (int i=0;i<digitCount/2;i++) {
char c = str[i]; char c = str[i];
@ -105,6 +109,8 @@ struct String add(struct String numb1, struct String numb2){
str[digitCount-i-1]=c; str[digitCount-i-1]=c;
str[i]=c2; str[i]=c2;
} }
str = realloc(str,digitCount+1);
str[digitCount]='\0';
struct String newStr = {digitCount,str}; struct String newStr = {digitCount,str};
return newStr; return newStr;
} }
@ -126,3 +132,9 @@ void printIntDoubleArr(int a,int b,int doubleArr[a][b]) {
printf("\n"); printf("\n");
} }
} }
struct String createBigNumber(char*numb) {
int marker=0;
while (numb[marker++]!='\0');
return (struct String){marker-1,numb};
}

View File

@ -10,3 +10,5 @@ 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);
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);
#define BigNumber(X) createBigNumber(#X)