1. Time complexity of exhaustive algorithm (O (n))
//From small to large
Public Static intgcdintMintN) {intGCD =1; for(inti =2; I <= m && i <= N; i++) { if(m% i = =0&& n% i = =0) {GCD=i; } } returnGCD; }
From big to small
Public Static int gcd (intint N) {
int 1 ; for (int1; i--) { if00) { = i; Break ; } } return gcd; }
//Exhaustive algorithm the divisor of number n is not likely to be larger than N/2
Public Static intgcdintMintN) {intGCD = 1; if(m% n = = 0) { returnN; } for(inti = N/2; I >= 1; i--) { if(m% i = = 0 && n% i = = 0) {GCD=i; Break; } } returnGCD; }
2. Recursive time complexity (O (LOGN))
Public Static int gcd (intint n) { if (m% n = = 0) {return c11> n; } Else { return gcd (n, m% N); } }
3, while cycle time complexity (O (LOGN))
Public Static int gcd (intint n) { int remainder = m% N; while (Remainder > 0) { = n; = remainder; = m% n; } return n; }
Beg Greatest common divisor