I want to find a multiplication of two numbers in Java recursively with using only Addition, Subtraction and Comparison. So, I googled and I found Egyptian Algorithm that meets the question requirement.
However, I’m not sure how to find the multiplication result after we reach the base case.
Example:
13 x 30
1 -- 30
2 -- 60
4 -- 120
8 -- 240 //we stop here because the double of 8 is larger than 13
To find the result we add the numbers from the left column that equals 13 which they are 1+4+8 and on the other hand we add its opposite numbers from the right column which they are 30+120+240 = 390 which is the result.
But now how to do last part programatically ? how to check which numbers to add? I hope you guys get my point. Hints only needed.
Okay, I did it using Brute Force Algorithm. It’s a BigO-(n) though. I’m sure there is more efficient way to implement it. For now. I’m settling with this.
Thanks guys!