20 complete!
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
e580693d7a
commit
ecce626376
BIN
archives/20/current
Executable file
BIN
archives/20/current
Executable file
Binary file not shown.
54
archives/20/src/main.c
Normal file
54
archives/20/src/main.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "utils.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
n! means n × (n − 1) × ... × 3 × 2 × 1
|
||||||
|
|
||||||
|
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
|
||||||
|
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
|
||||||
|
|
||||||
|
Find the sum of the digits in the number 100!
|
||||||
|
|
||||||
|
|
||||||
|
https://projecteuler.net/problem=20
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
int digitCount(int numb) {
|
||||||
|
int digitCount=1;
|
||||||
|
while (numb>=10) {
|
||||||
|
numb/=10;
|
||||||
|
digitCount++;
|
||||||
|
}
|
||||||
|
return digitCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
char*numberToString(int numb) {
|
||||||
|
char*num=malloc(digitCount(numb));
|
||||||
|
int counter=digitCount(numb)-1;
|
||||||
|
while (numb!=0) {
|
||||||
|
num[counter--]=(numb%10)+'0';
|
||||||
|
numb/=10;
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc,char**argv) {
|
||||||
|
struct String factorialSum = {1,"1"};
|
||||||
|
int counter=2;
|
||||||
|
while (counter<=100) {
|
||||||
|
factorialSum=mult(factorialSum,(struct String){digitCount(counter),numberToString(counter)});
|
||||||
|
printf("%s\n",factorialSum.str);
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s",factorialSum.str);
|
||||||
|
int digitSum=0;
|
||||||
|
for (int i=0;i<factorialSum.length;i++) {
|
||||||
|
digitSum+=factorialSum.str[i]-'0';
|
||||||
|
}
|
||||||
|
printf("\nThe sum of the digits are %d",digitSum);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
128
archives/20/src/utils.c
Normal file
128
archives/20/src/utils.c
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
#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(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (carryover>0) {
|
||||||
|
str = realloc(str,++digitCount);
|
||||||
|
str[digitCount-1]='1';
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
12
archives/20/src/utils.h
Normal file
12
archives/20/src/utils.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#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]);
|
36
src/main.c
36
src/main.c
@ -1,5 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
n! means n × (n − 1) × ... × 3 × 2 × 1
|
n! means n × (n − 1) × ... × 3 × 2 × 1
|
||||||
@ -12,13 +13,42 @@
|
|||||||
|
|
||||||
https://projecteuler.net/problem=20
|
https://projecteuler.net/problem=20
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
int digitCount(int numb) {
|
||||||
|
int digitCount=1;
|
||||||
|
while (numb>=10) {
|
||||||
|
numb/=10;
|
||||||
|
digitCount++;
|
||||||
|
}
|
||||||
|
return digitCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
char*numberToString(int numb) {
|
||||||
|
char*num=malloc(digitCount(numb));
|
||||||
|
int counter=digitCount(numb)-1;
|
||||||
|
while (numb!=0) {
|
||||||
|
num[counter--]=(numb%10)+'0';
|
||||||
|
numb/=10;
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc,char**argv) {
|
int main(int argc,char**argv) {
|
||||||
struct String factorialSum = {1,"0"};
|
struct String factorialSum = {1,"1"};
|
||||||
int counter=1;
|
int counter=2;
|
||||||
printf("%s",mult((struct String){14,"85018361849301"},(struct String){7,"1849204"}).str);
|
|
||||||
while (counter<=100) {
|
while (counter<=100) {
|
||||||
|
factorialSum=mult(factorialSum,(struct String){digitCount(counter),numberToString(counter)});
|
||||||
|
printf("%s\n",factorialSum.str);
|
||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("%s",factorialSum.str);
|
||||||
|
int digitSum=0;
|
||||||
|
for (int i=0;i<factorialSum.length;i++) {
|
||||||
|
digitSum+=factorialSum.str[i]-'0';
|
||||||
|
}
|
||||||
|
printf("\nThe sum of the digits are %d",digitSum);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
12
src/utils.c
12
src/utils.c
@ -20,7 +20,7 @@ struct String mult(struct String numb1, struct String numb2) {
|
|||||||
carryover=0;
|
carryover=0;
|
||||||
for (int j=n1.length-1;j>=0;j--) {
|
for (int j=n1.length-1;j>=0;j--) {
|
||||||
int mult = (n1.str[j]-'0')*(n2.str[i]-'0')+((carryover!=0)?carryover:0);
|
int mult = (n1.str[j]-'0')*(n2.str[i]-'0')+((carryover!=0)?carryover:0);
|
||||||
printf(" %d/%d\n",mult,carryover);
|
//printf(" %d/%d\n",mult,carryover);
|
||||||
carryover=0;
|
carryover=0;
|
||||||
if (mult>=10) {
|
if (mult>=10) {
|
||||||
carryover=mult/10;
|
carryover=mult/10;
|
||||||
@ -32,7 +32,7 @@ struct String mult(struct String numb1, struct String numb2) {
|
|||||||
addends[(n2.length-1)-i][0]=carryover;
|
addends[(n2.length-1)-i][0]=carryover;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printIntDoubleArr(n2.length,n1.length+1,addends);
|
//printIntDoubleArr(n2.length,n1.length+1,addends);
|
||||||
struct String sum = {1,"0"};
|
struct String sum = {1,"0"};
|
||||||
for (int i=0;i<n2.length;i++) {
|
for (int i=0;i<n2.length;i++) {
|
||||||
char val[n1.length+1+i];
|
char val[n1.length+1+i];
|
||||||
@ -45,6 +45,14 @@ struct String mult(struct String numb1, struct String numb2) {
|
|||||||
sum=add((struct String){n1.length+1+i,val},sum);
|
sum=add((struct String){n1.length+1+i,val},sum);
|
||||||
//printf("%s\n",sum.str);
|
//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);
|
//printf("%s",sum.str);
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user