最大公约数
Question:
computing the greatest common divisor
Solution:
1 template <typename T> 2 T gcd(T m,T n) 3 { 4 if(m<n) 5 { 6 T temp = a; 7 a=b; 8 b=temp; 9 } 10 11 while(n!=0) 12 { 13 T rem=m%n; 14 m=n; 15 n=rem; 16 } 17 return m; 18 }
Analysis:
采用辗转相除法求最大公约数,算法复杂度为O(logN)
Author: Cat