0

[Challenge] Colud you write a program that give a sum of tow 20 digit number?

Numbers can have less than a 20 digit

18th Dec 2017, 6:44 PM
Ahmad.Fz
Ahmad.Fz - avatar
5 Antworten
+ 1
Hi, to solve this problem you should consider 'divide and conquer' strategy and working with string(of digits). If both inputs have length of 20 digits, it's easier to solve it. With a recursive function, you just split each input into two string and call your function for respected splited inputs. But for two random input with totally random length, it's kind of challenging. if you have any questions feel free to ask.
18th Dec 2017, 7:11 PM
Rasul Abbasi
Rasul Abbasi - avatar
+ 1
yep array is the ansewer. but you didnt mention the carry. we should have solution for numbers with diffrent lengthes.
18th Dec 2017, 8:16 PM
Ahmad.Fz
Ahmad.Fz - avatar
0
That's right. It was part of implementation not the strategy, which I was focused on. In the recursive call you should consider the carry. How to detect if a result has carry? By checking the length of recursive call result. if it's more than the length of one the inputs, then the first digit is the carry , and you must apply logic to handle the carry.
18th Dec 2017, 8:23 PM
Rasul Abbasi
Rasul Abbasi - avatar
0
Working with array of int ( if that's what you meant by 'array') might be more difficult because of the carry and other problems.
18th Dec 2017, 8:26 PM
Rasul Abbasi
Rasul Abbasi - avatar
0
yes thnx. that's a good solution and posible to achive with loops too. and loop can solve the length problem.
18th Dec 2017, 8:37 PM
Ahmad.Fz
Ahmad.Fz - avatar