diff --git a/archives/32/current b/archives/32/current new file mode 100755 index 0000000..e531a5b Binary files /dev/null and b/archives/32/current differ diff --git a/archives/32/src/main.c b/archives/32/src/main.c new file mode 100644 index 0000000..ddb7be3 --- /dev/null +++ b/archives/32/src/main.c @@ -0,0 +1,76 @@ +#include +#include "utils.h" +#include + +/* + We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital. + + The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital. + + Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital. + + HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum. + + https://projecteuler.net/problem=32 +*/ + +int main(int argc,char**argv) { + int numb1=1; + int numb2=1; + long sumPandigital=0; + boolean*uniqueProducts = malloc(100000000); + for (int i=0;i<100000000;i++) { + uniqueProducts[i]=false; + } + for (numb1=1;numb1<10000;numb1++) { + for (numb2=1;numb2<10000;numb2++) { + int prod=numb1*numb2; + int numb=prod; + boolean uniqueDigits[9] = {}; + while (numb>0) { + int digit=numb%10-1; + if (digit!=-1&&!uniqueDigits[digit]) { + uniqueDigits[digit]=true; + } else { + goto skip; + } + numb/=10; + } + numb=numb1; + while (numb>0) { + int digit=numb%10-1; + if (digit!=-1&&!uniqueDigits[digit]) { + uniqueDigits[digit]=true; + } else { + goto skip; + } + numb/=10; + } + numb=numb2; + while (numb>0) { + int digit=numb%10-1; + if (digit!=-1&&!uniqueDigits[digit]) { + uniqueDigits[digit]=true; + } else { + goto skip; + } + numb/=10; + } + for (int i=0;i<9;i++) { + if (!uniqueDigits[i]) { + goto skip; + } + } + //If we got here it's a pandigital. + if (!uniqueProducts[prod]) { + uniqueProducts[prod]=true; + sumPandigital+=prod; + printf("\n%d*%d = %d is pandigital!",numb1,numb2,prod); + } + skip: + continue; + } + } + printf("\nSum of pandigitals: %ld",sumPandigital); + return 0; +} diff --git a/archives/32/src/utils.c b/archives/32/src/utils.c new file mode 100644 index 0000000..b20be5d --- /dev/null +++ b/archives/32/src/utils.c @@ -0,0 +1,207 @@ +#include "utils.h" +#include +#include + +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=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=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]+((carryover>0)?carryover--:0); + } + //str[offset+1]='\0'; + } + } else { + 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]=numb2.str[numb2.length-offset-1]+((carryover>0)?carryover--:0); + } + //str[offset+1]='\0'; + } + } + if (carryover>0) { + str = realloc(str,++digitCount); + str[digitCount-1]='1'; + //str[digitCount]='\0'; + } + for (int i=0;i0) { + newStr=realloc(newStr,++strLength); + newStr[strLength-1]=(numb1%10)+'0'; + numb1/=10; + } + for (int i=0;i #include "utils.h" +#include /* - In the United Kingdom the currency is made up of pound (£) and pence (p). There are eight coins in general circulation: + We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital. - 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p). + The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital. - It is possible to make £2 in the following way: + Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital. - 1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p + HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum. - How many different ways can £2 be made using any number of coins? - - - https://projecteuler.net/problem=31 + https://projecteuler.net/problem=32 */ -enum Currency{ - pence1, - pence2, - pence5, - pence10, - pence20, - pence50, - pound1, //100 pences - pound2, //200 pences -}; - -int getValue(enum Currency cur) { - switch (cur) { - case pence1:{ - return 1; - }break; - case pence2:{ - return 2; - }break; - case pence5:{ - return 5; - }break; - case pence10:{ - return 10; - }break; - case pence20:{ - return 20; - }break; - case pence50:{ - return 50; - }break; - case pound1:{ - return 100; - }break; - case pound2:{ - return 200; - }break; - } -} - -int total(int*amts) { - return - getValue(pence1)*amts[0]+ - getValue(pence2)*amts[1]+ - getValue(pence5)*amts[2]+ - getValue(pence10)*amts[3]+ - getValue(pence20)*amts[4]+ - getValue(pence50)*amts[5]+ - getValue(pound1)*amts[6]+ - getValue(pound2)*amts[7]; -} - -void carryover(int*amts,int placeVal) { - amts[placeVal]++; - if (placeVal<7&&amts[placeVal]>200/getValue(placeVal)) { - carryover(amts,placeVal+1); - amts[placeVal]=0; - } -} - int main(int argc,char**argv) { - int currencyAmts[8] = {}; - int combinationCount=0; - int maxCurrencyVal=200; - int currentMarker=0; - - while (currencyAmts[6]!=2) { - if (total(currencyAmts)==200) { - printf("Coin amts: "); - for (int i=0;i<8;i++) { - printf("%d ",currencyAmts[i]); + int numb1=1; + int numb2=1; + long sumPandigital=0; + boolean*uniqueProducts = malloc(100000000); + for (int i=0;i<100000000;i++) { + uniqueProducts[i]=false; + } + for (numb1=1;numb1<10000;numb1++) { + for (numb2=1;numb2<10000;numb2++) { + int prod=numb1*numb2; + int numb=prod; + boolean uniqueDigits[9] = {}; + while (numb>0) { + int digit=numb%10-1; + if (digit!=-1&&!uniqueDigits[digit]) { + uniqueDigits[digit]=true; + } else { + goto skip; + } + numb/=10; + } + numb=numb1; + while (numb>0) { + int digit=numb%10-1; + if (digit!=-1&&!uniqueDigits[digit]) { + uniqueDigits[digit]=true; + } else { + goto skip; + } + numb/=10; } - printf("\n"); - combinationCount++; + numb=numb2; + while (numb>0) { + int digit=numb%10-1; + if (digit!=-1&&!uniqueDigits[digit]) { + uniqueDigits[digit]=true; + } else { + goto skip; + } + numb/=10; + } + for (int i=0;i<9;i++) { + if (!uniqueDigits[i]) { + goto skip; + } + } + //If we got here it's a pandigital. + if (!uniqueProducts[prod]) { + uniqueProducts[prod]=true; + sumPandigital+=prod; + printf("\n%d*%d = %d is pandigital!",numb1,numb2,prod); + } + skip: + continue; } - carryover(currencyAmts,0); } - printf("\n\nCombination count: %d",(combinationCount+2)); - + printf("\nSum of pandigitals: %ld",sumPandigital); return 0; -} \ No newline at end of file +}