What I am trying is write recursive function which returns the least common divisor or for example let’s take 150 and 125, the greatest common divisor is 25 while the least common divisor is 5. Once again I need a recursive function in direct method it is trivial.
What I am trying is write recursive function which returns the least common divisor
Share
Test every number until
sqrt(min(a, b)): if the numbers are both divisible by it, you found it. You can only test primes if you want.If you haven’t found any such number, then check if the other number is a multiple of the minimum: if yes, the minimum of the two is the solution. Otherwise, there’s no solution.
You can do better. You can go only up to
sqrt(gcd(a, b)). That should be fast enough.