Home > OS >  How to solve greatest common devisor using recursion?
How to solve greatest common devisor using recursion?

Time:12-11

Need to solve the problem to find Greatest Common Devisor or HCF of two integers using recursion.

I did solve the solution but using while loop.

CodePudding user response:

int HCF(int a, int b) { while (a != b) { if (a > b) { return HCF(a - b, b); } else { return HCF(a, b - a); } } return a; }

CodePudding user response:

For recursion based this will work, May be there are more edge cases, then I need to correct the solution.

def gcd(low, high):
  if low<=0:
    return high
  return gcd(high%low, low)
  • Related