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


i'm trying add gcd() function numericfunctions class , include code in main compute gcd(m,n).

however, keep getting error:

exception in thread "main" java.lang.stackoverflowerror     @ numericfunctions.gcd(numericfunctions.java:14) 

source code:

public class numericfunctions {     public static long factorial(int n) {        long result = 1;        (int = 2; <= 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) {           (int n = 1; n <= 10; n++)              (int m = 1; m <= 10; m++){                 system.out.println(gcd(n,m));                 system.out.println(" ");                 system.out.println(factorial(n));              }       } } 

check out following corrections in gcd() method:

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

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -