一個簡單的小算法來獲取兩個數的最大公約數,
1 public class Test { 2 public static void main(String[] args) { 3 long result = gcd(15, 3); 4 System.out.println(result); 5 } 6 7 public static long gcd(long m, long n) { 8 while (n != 0) { 9 long rem = m % n; 10 m = n; 11 n = rem; 12 } 13 return m; 14 15 } 16 }
