John
John

Reputation: 21

What is the result of this recursive function

What does this recursive function return?

def fun(a,b):

      if(b==0):

            return a

      else:

            return fun(b, a%b)

I tried checking on some numbers for example it returns 3 for 15,6

Upvotes: 0

Views: 39

Answers (1)

QWERTYL
QWERTYL

Reputation: 1373

This calculates the greatest common divisor between a and b.

See this question for the proof: https://math.stackexchange.com/questions/59147/why-gcda-b-gcdb-a-bmod-b-understanding-euclidean-algorithm

The greatest common divisor (gcd) of two numbers a and b is the largest number that divides both a and b.

Note: f(6, 15) should return 3, as 3 is the largest number that divides both 6 and 15

Upvotes: 3

Related Questions