You have to go through a cycle to find this, and it can help if you put your algorithm with some equations, how it should work.
But you have two problems that I see if you do not call this inside another loop.
You return in both cases, either if or otherwise, so you only go here once.
In addition, this part does not make sense, why change the value of b
after doing return
?
return b; b = b%a;
You must use recursion for this, btw.
http://rosettacode.org/wiki/Greatest_common_divisor#Recursive_Euclid_algorithm
source share