There's probably some extra checking to do on the flip side for numbers that are not the exact same length
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
b2d68f77ef
commit
1a6e740f28
18
src/main.c
18
src/main.c
@ -2,26 +2,12 @@
|
||||
#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
|
||||
https://projecteuler.net/problem=
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
16
src/utils.c
16
src/utils.c
@ -23,7 +23,21 @@ struct String add(struct String numb1, struct String numb2){
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
for (int offset=0;offset<numb1.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];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (carryover>0) {
|
||||
str = realloc(str,++digitCount);
|
||||
|
Loading…
x
Reference in New Issue
Block a user