0

I'm trying to compute the combination

procedure(factorial(n)/(factorial(r)*factorial(n-r)).

When I call the factorial method from within the combination method, when running I get a stackoverflow error before the calculation.

The instructions read "This method should receive two integers as its parameters, n and r. Your combination method should call the factorial method inside its definition by using it in the following formula:"

Any help will be appreciated we haven't really covered stackoverflow so I'm a little lost. Thank you!!

import java.util.Scanner;
public class lab6_Perea{
public static void main(String[] args){
    Scanner sc = new Scanner (System.in);
    System.out.println("Hello! Welcome to Set calculator. Please select the calculation you want to perform \n\t1. Power \n\t2. Factorial \n\t3. Combination \n\t4. Permutation");
    int option = sc.nextInt();

    switch(option){
        case 1:
            System.out.println("Please enter the base and exponent: ");
            int x = sc.nextInt();
            int y = sc.nextInt();
            System.out.print("The answer is: " + power(x, y));
        break;

        case 2:
            System.out.println("Please enter a number: ");
            int z = sc.nextInt();
            System.out.print("The facotrial of " + z + " is " + factorial(z));
        break;   

        case 3:
            System.out.println("Please enter 2 numbers to calculate the Combination: ");
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.print("The total combination is: " + combination(a, b));
        break;

        case 4:
            // FIX ME call Combination method
        break;
    }

}
public static int power(int n, int m){

    if(m==0)
        return 1;
    else
        return (n * power(n, m-1));  
}

public static int factorial(int n){
    if(n == 0)
        return 0;
    else
        return (n * factorial(n-1));
}
public static int combination ( int n, int r){
    int combo =factorial(n)/(factorial(r)*factorial(n-r));
    return combo;
}

}

Trushit Shekhda
  • 563
  • 7
  • 18
Jonathan
  • 11
  • 4

3 Answers3

0

Use this function:

public static int factorial(int n){
    if(n <= 1)
        return 1;
    else
        return (n * factorial(n-1));
}

And you do not have to test n for positivity. The recursion you wrote went in StackOverflow because n-r<0 and your original rule (if n==0) was never encountered.

robob
  • 1,739
  • 4
  • 26
  • 44
-1

Use return 1 if n==0

public static int factorial(int n){
    if(n == 0)
        return 1;
    else
        return (n * factorial(n-1));
}

Please check n>=r condition.

Hope, this will work!

Birju B
  • 140
  • 1
  • 5
-1

It's because there is some limit on the number of recursions one can use. To overcome it, you can use a for loop inside the factorial function.

Major_Ash
  • 199
  • 3
  • 16
  • No the first problem is that the functional function is wrong because it always returns 0. The correct function is: public static int factorial(int n){ if(n == 1) return 1; else return (n * factorial(n-1)); } – robob Apr 08 '20 at 05:48
  • Even if you correct that, you will still get stackoverflow error. To avoid that you can just use for loop with condition n>0 – Major_Ash Apr 08 '20 at 06:20
  • Yes you can test if n<1 then 1 and recursion and if n – robob Apr 08 '20 at 06:40