如何编写一个函数来实现欧几里得算法来计算最大公约数(m,n)?

How to write a function that implements Euclid's Algorithm for computing the greatest common divisor ( m,n)?

我正在尝试将 gcd() 函数添加到 NumericFunctions class 并在 main 中包含代码以计算 gcd(m,n)

但是,我一直收到错误消息:

Exception in thread "main" java.lang.WhosebugError
    at NumericFunctions.gcd(NumericFunctions.java:14)

源代码:

public class NumericFunctions {

   public static long factorial(int n) {

      long result = 1;

      for (int i = 2; i <= n; i++) {

         result *= i;
      }
      return result;
   }

   public static int gcd (int n, int m) {

      if ((m % n) == 0) 

         return n;

      else

         return gcd(n, m % n);
}

     public static void main(String[] args) {

         for (int n = 1; n <= 10; n++)

            for (int m = 1; m <= 10; m++){

               System.out.println(gcd(n,m));

               System.out.println(" ");

               System.out.println(factorial(n));

            }
      }
}

检查 gcd() 方法中的以下更正:

public static int gcd (int n, int m) {
    if ((n % m) == 0)
        return m; // <-- first correction
    else
        return gcd(m, n % m);  // <-- second correction
}