940 views
2 votes
2 votes
Could anyone explain the complexity of function (finding GCD) using modulo operator

1 Answer

1 votes
1 votes

Lets see the code of finding GCD using modulo first.Here let us say , a > b , so at 1st function call gcd(a,b) , a is the dividend and b is the divisor.

function gcd(a, b)
    if b = 0
       return a;
    else
       return gcd(b, a mod b);

So we know that the division step takes logarithmic time.Take an example :

Let we have a number , say n = 8.

We divide it by 2 till we get n = 1.So no. of steps involved ,

                              =  n / 2k = 1

                              = k   =   O(logn)

So we can see that division operation takes O(logn) time.Simlarly in the above method of finding GCD , we are using the remainders as the divisor in the next step and the current divisor as the next dividend.So using modulo method is undergoing division method only.

Hence the complexity of this method is O(loga)  [Since we have already assumed a > b].

Related questions

0 votes
0 votes
0 answers
1
Mizuki asked Nov 20, 2018
275 views
What is −6 mod 18?Please elaborate.
2 votes
2 votes
2 answers
2
Vikrant Singh asked Jan 31, 2015
1,726 views
What is the worst case time complexity to find the gcd(m,n) using best algorithm known?A. O(log(min(m,n)))B, O(log(max(m,n)))
1 votes
1 votes
2 answers
3
radhika khandelwal asked Dec 1, 2016
1,214 views
What will be the remainder when $\large 6457^{76^{57}}$ is divided by $\large 23$ ?
1 votes
1 votes
2 answers
4
Nilam asked Oct 19, 2015
3,375 views
What are the generators for the group G={1,2,3,4,5,6} having multiplication modulo 6 as an operation?