Return the Sum of Two Numbers (With a Twist)

Published by Deep Xavier in

Wait, a very hard challenge for returning the sum of two numbers? Well in this one, the numbers can have 1000 digits!

So, what's the twist? You have to do the summation as if you're doing it manually on a piece of paper, thus, the conversion of the numeric string to numeric literal is basically disallowed.

Create a function that:

  • Takes two numbers as strings.
  • Calculates the sum of these two numbers.
  • Returns the result as a string.

Examples

sum("12132000", "12171979") ➞ "24303979"

sum("4666", "544") ➞ "5210"

sum("1521512512512512515", "898989898989988998899898") ➞ "898991420502501511412413"

sum("5125515215521515", "125261616261626") ➞ "5250776831783141"

sum("6666666666666666666666666666", "99999999999999999999999") ➞ "6666766666666666666666666665"

sum("123456789123456789123456789", "987654321987654321987654329876543") ➞ "987654445444443445444443453333332"

Notes

  • All numbers are positive integers with no leading zeros.
  • Remember how to sum two numbers ON A PAPER, such is the process.
  • Your function must run in less than 10 seconds because Edabit has a time limit.
  • The use of Number classes such as BigInteger or BigDecimal is disallowed -- it will defeat the purpose and the level of difficulty associated to this challenge.
  • A recursive version of this challenge can be found via this link.
Watch a quick demo on how Edabit works.