2

The algorithm I do not understand is :

alg(m, n)
1. if m>n then
2.     return alg(m-n, n)
3. else
4.     if n>m then
5.          return alg(n, m)
6.     else
7.          return n

I think that the recurrence formula is T(m) = T(m-n) + a, where a is a constant. I tried to do a substitution:

T(m) = T(m-k) + a*n

Assume that

k=m => T(m) = T(0) + a*n => T(m) = n*a + 1 => The complexity is O(n).

Please tell me if I am wrong. Thank you!

Nicolás Alarcón Rapela
  • 2,714
  • 1
  • 18
  • 29
Cata
  • 39
  • 7
  • This algorithm computes the greatest divisor of two integers. For example, if i consider m = 120 and n = 75, the algorithm returns 15. m is the first integer and n is the second integer. – Cata Jun 14 '18 at 09:44
  • If m = -1, n = 1 then the recursion never finishes. I guess your numbers are positive integers, right? – Anatolii Jun 14 '18 at 09:55
  • Yes, I forgot to say that – Cata Jun 14 '18 at 10:01

1 Answers1

0

I searched on the Internet and the complexity of this algorithm is O(n+m). I am posting this to help other people. The link is: https://codility.com/media/train/10-Gcd.pdf

Cata
  • 39
  • 7