From b2d68f77ef3c4ca4a86f27e052237934eac3daab Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Mon, 18 Jul 2022 05:52:59 -0500 Subject: [PATCH] Completed problem 16! Implemented big number utils Co-authored-by: sigonasr2 --- archives/16/current | Bin 0 -> 16104 bytes archives/16/src/main.c | 27 +++++++++++++++++++++++++++ archives/16/src/utils.c | 40 ++++++++++++++++++++++++++++++++++++++++ archives/16/src/utils.h | 9 +++++++++ current | Bin 16040 -> 16104 bytes src/main.c | 18 ++++++++++++++++-- src/utils.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/utils.h | 4 +++- 8 files changed, 135 insertions(+), 3 deletions(-) create mode 100755 archives/16/current create mode 100644 archives/16/src/main.c create mode 100644 archives/16/src/utils.c create mode 100644 archives/16/src/utils.h create mode 100644 src/utils.c diff --git a/archives/16/current b/archives/16/current new file mode 100755 index 0000000000000000000000000000000000000000..72e418b8e5db96c62cfc305946303b02bb3fd0ed GIT binary patch literal 16104 zcmeHOe~c7Y9e=YFI9kdc1uUhttQDHpddnTZgv!|kZeY$F6yY|drgge^yLY!{f1JCs z_GkdnwT5xI1f$XZp$%%%5;f*xOvNqvVOkv-glWB z_G;49_|Lp#_w&9#-}imro7p!z?|nh<*b>k*!DWW{gdpljqvS-C;J%Nk3LqlZiCgi# zRNO6QfxgM%NqIy9q_Uh3OvMJpp8+Jh#Z;+-cS*Kjh@Io~ymkOB&$+m1px+T@7E(blQhr}EOTknM;hyJO1kn6hJ>Q0*~t zJ}G?qtW){)Vl!k^7`=9V%Fc^z(o8T?8&r0^ZpGhAdpnff>X73`cyWVd3r245Q?OG* zt2Evnc$jyp_NE($-75c#D!pPwD%rJm^@>!yDV59=2AT%et!-MnI-JjjS2_(-Tp@gD zPHo*D6>u|5Je5WfI3Q{EN3w-VPWrDb7SF|Ajhq&h&n8=*eDTxcf#3arbSQ4JAss5@ zkEaOHRHv~c9hr|Wq9%ESXA{3p6HZrdhv2q4!biai)#0~*S5Ln%jXuR+Pyh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!r%?fXMgm9bp2lofCLuY--< ztF<#>e9n!}WqS3QW_$~_L6<$R+lQLKL}5KiU8kA<4g@rJd6; +#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 +#include + +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;offsetoffset) { + //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;isxf9dqfJb71^3L%N?Z&W{=fudaf=Q#Sc!Q3&V397Jj>4a`kn7} zPS3fCo=nfca{Hi-C?lj2vMz9R!wJi=992Aa^sG0p&7=x#Ca_aaO^B+ZPKoA$soAo` zM%A9E{x98QQ00BlM;%;JLnSp-5USG3rv~rgR7>77D|lx$7V_l zq#VO(4IALqi=to!;}rWw)nOOQ80eU8IvW@>GuZ?NVJ_vLh1-n8AF`-Yd0%W{zf8*Cf4DO+~= zVoH|ff#0pGuc9H2cB^X}A)3tJBv;=gm%#zsJq=SBQ!Xq(+aJ3;R@bXa3PQX(h#Dbo zE@32?jTQ=$`HBbm*G8fi`MlM2m;p~|6!8`XUuJ+g+bTK=x!mgYqlhQn#bF^<{T^cl z@uz6MAXT40A@kQivYctf^vBHaz@uDIuTShbCy3qYasC23665>=BbdUIce15?cJ5v& zydX&RS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9F{-QPL) z{O-B;oO|zi>Avy4k>bJuj!{~R3QT(M=&sM%OJ-HQH2R}6cfp`43kE18j@DA5syZ^7 z2RclrC2CZA!uL2H)!cN$AhGgEjg{0`N%)f7=Q+DNXB|N@1j_!;qhIsr2R7@h#n<-9 z(g%)M)mz7|e$2jqfz(tdI^n&ns-kT4sc(*929*`EVJ@o@{KjI^1%pN_DTmc8E2&fZ zL1knF`{wdMpeKCnc;NL&@LVKtIv75~1gLWSRN#2`slbU~xCc_korXQkSbZf_nB4tF zT#7J8H4K!p7%$3NikRruFw3Cz8EwoG4(Bt-@oW06(T^~N2IduQzMjJzO+U)3!lafp zecY~%=fITdr4p0Irm)hiD)eN`P-C{j)8;(r#_v^g9y_mDvqx{*CI&3s>=;*`Gv;KN zU*!sbUEHpxQ1}$}0vm4mw}S0|qJCt9-T`rLhrbz9EEmDXNLZoH#bFHhg;Y#P zEAcG;Ez4>KmZfPt4tniGX=ajA)3blcvLYcP6B2h@lmH^8^k-<8yUk2=TE;g`TsD75 z#_Y!S+}w(gx1uLRr;=@1jZKL3c5y4R+>6+P7)SIYCJ=iOI}s1B%JMAYJwzobaN6Ez zivJ?ZF8JEMgItCMyH(Nc@Uz`o8$~W7Ji%QwwB?%(J$czV3{#3j*YR6KpG%L~AE14r zwd`abTsGI0=Ez%C=PXIU6_>Rn#i?|? zuS3Crg~1LBE+;Nd+}8DN>zqp{5T*z>=yq2^uiH+FVbtv;Nti;mULR&So0f&)w&ybk?wO{!BAI-mT>rL&)b!Ou23)%WCAC`XTtxg=`GJ4 zY(LNzsCDcCPyIn-pzBNsZqy$(o<93V*MxcR5i-&3IZE&t=Jq-HxKUkM$7|5)+6<)) zrThV{5bMZ6dxMkj)JAn|0ja^s# +#include + +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;offsetoffset) { + //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