Co-authored-by: sigonasr2 <sigonasr2@gmail.com>main
parent
311721685d
commit
b2d68f77ef
Binary file not shown.
@ -0,0 +1,27 @@ |
||||
#include <stdio.h> |
||||
#include "utils.h" |
||||
|
||||
/*
|
||||
215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. |
||||
|
||||
What is the sum of the digits of the number 21000? |
||||
|
||||
|
||||
https://projecteuler.net/problem=16
|
||||
*/ |
||||
|
||||
int main(int argc,char**argv) { |
||||
|
||||
int i=1; |
||||
struct String numb = {1,"2"}; |
||||
while (i++<1000) { |
||||
numb = add(numb,numb); |
||||
} |
||||
//printf("%s",numb.str);
|
||||
int sum=0; |
||||
for (int j=0;j<numb.length;j++) { |
||||
sum+=numb.str[j]-'0'; |
||||
} |
||||
printf("The sum of the digits of %s is %d",numb.str,sum); |
||||
return 0; |
||||
} |
@ -0,0 +1,40 @@ |
||||
#include "utils.h" |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
struct String add(struct String numb1, struct String numb2){ |
||||
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]; |
||||
} |
||||
} |
||||
} else { |
||||
|
||||
} |
||||
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; |
||||
} |
@ -0,0 +1,9 @@ |
||||
#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); |
@ -0,0 +1,40 @@ |
||||
#include "utils.h" |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
struct String add(struct String numb1, struct String numb2){ |
||||
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]; |
||||
} |
||||
} |
||||
} else { |
||||
|
||||
} |
||||
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; |
||||
} |
@ -1,7 +1,9 @@ |
||||
#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); |
Loading…
Reference in new issue