Monday, July 8, 2013

[Project Euler] Problem 16 in C

Problem: Project Euler Problem 16
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?
About the Problem
This problem just involves dealing with large numbers without losing precision.

Solving the Problem
This problem was not particularly hard since we have dealt with large numbers before (such as adding) like in  Problem 13.

I have just created a function where it takes two strings of numbers and multiply them to output/return a value   by reference.
void multiplyTwoNumbers(char *prod, char *num1, char *num2, int numDigits);

prod represents the result of multiplication between num1 and num2.
numDigits represent the number of digits I want to see.

Code:


Challenges Faced (Logic behind Code)
Unlike the code found in my Problem 13 solution, I have reversed the order of the numbers. This means ones digit is in char[0], tens digit is in char[1]. So, instead of writing a hundred twenty(120), the code would write (021).
I have done this because I would not need to know the length of the number in order to perform a multiplication of 2. Therefore, the code would not go through the same array twice, improving the performance.

Lastly, the only the sum of digits, which has associative property, was needed, so the ordering did not matter.

Next Step
N/A

Questions
N/A

__________________________________________________________________
Best Execution Time: 0.016s
Answer:

No comments:

Post a Comment