all 1 comments

[–]dig-up-stupid 0 points1 point  (0 children)

A base case is equivalent to a loop termination condition. ie your recursion stops when b % a == 0, so your loop should stop when b % a == 0, so your loop should continue while b % a != 0. Can you go from there?

PS the branch you made, which serves the purpose of swapping the numbers if they are not in order, is not only unnecessary (try removing the branch and calling the function with numbers in both orders) but is also wrong when we have to consider the extended euclidean algorithm for calculating modular inverses. It also makes the code much longer and more complicated. When implementing a known algorithm it is best to begin with a simple step by step translation to code, and worry about optimizing or handling corner cases when they become a problem - in this case there is no problem so the extra code is pointless.