Wednesday, July 3, 2013

[Project Euler] Problem 13 in C

Problem: Project Euler Problem 13
Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
About the Problem
The problem asks us to find the first ten digits of a sum of large numbers.
For a moment, I thought the question was too easy to be #13 of Project Euler but I was wrong since I was solving in C.
Since it seems project euler likes to deal with large numbers, I decided to just create a user-friendly function for adding string consisting of only numbers together for future uses.

Solving the Problem
After I have solved the problem, I quickly looked through the answer forum that is only accessible to the ones who have solved the problem. I was flabbergasted by BigInteger library that was available to a lot of languages except for C!
They had literally used only one line to solve the problem which made me a sad panda.

Anyways, I have solved this problem by first assigning the numbers to an array of strings.
Then I have added the strings altogether by adding the strings two at a time.
When I mean adding the strings together, I mean traversing through the string to add digit by digit by first transforming the digit character to an integer to use the + operator.

At first, I was thinking of adding ten large strings at a time because this way, I do not have to worry about two carrys since the maximum value we can get from adding 10 ones digit is 9 * 10 = 90 (where 9 is the carry and 0 is the sum).

However, I became lazy and decided to just add two strings at a time.

Therefore, I have created a function, addTwoNumbers, where it takes two strings of numbers and return the addition result. Although there are limitations of this function as I have quickly put them together, such as fixed length of addition result and requirement that num1 is longer in length than num2, the function works as intended.

After devising this function, the answer was easy to get.
All I needed to do was call this function in a loop to traverse through the entire array of strings I have parsed in the beginning of the program.

Without further ado, here is the code
Code:



Challenges Faced/ Lessons Learned
It has been a long time since I have dealt with C strings or in fact, any strings! So, I had to review on some of the concepts behind string pointers and char array for proper string manipulation.

Here are couple of things that have hindered me from arriving at the answer earlier!

  • Returning string from a function and assigning it to another variable
  • Initializing values for a string after declaration
  • Assigning to a variable from an input file line by line at a time

Although I knew why the code was not working, I spent most of my time trying to find shortcuts in achieving the same results.

Next Step

  • Improve addTwoNumbers for more flexibility, such as non-fixed sized string addition, returning value by reference
  • Allow addition of 10 big integers at a time

Questions
I still have some questions left risen from solving this problem:

  • How can I easily initialize values for a string after declaring an array of characters? (Right now, I have a loop traversing through the array to assign a value to each element at a time)
  • Is there a safe, stable way of assigning line by line from an input file to a variable?

__________________________________________________________________
Best Execution Time: 0.010s
Answer:

No comments:

Post a Comment