Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factorial method - recursive or iterative? (Java)

I was making my way through project Euler, and I came across a combination problem. Combination logic means working out factorials. So, I decided to create a factorial method. And then I hit upon a problem - since I could quite easily use both iteration and recursion to do this, which one should I go for? I quickly wrote 2 methods - iterative:

public static long factorial(int num) {
        long result = 1;
        if(num == 0) {
            return 1;
        }
        else {
            for(int i = 2; i <= num; i++) {
                result *= i;
            }
            return result;
        }

and recursive:

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

If I am (obviously) talking about speed and functionality here, which one should I use? And, in general, is one of the techniques generally better than the other (so if I come across this choice later, what should I go for)?

like image 590
Bluefire Avatar asked Jun 23 '12 17:06

Bluefire


People also ask

Is factorial a recursive function?

The factorial function can be written as a recursive function call. Recall that factorial(n) = n × (n – 1) × (n – 2) × … × 2 × 1. The factorial function can be rewritten recursively as factorial(n) = n × factorial(n – 1).

Is recursive factorial faster than iterative?

Iteration is faster and more efficient than recursion. It's easier to optimize iterative codes, and they generally have polynomial time complexity.

What is factorial method in Java?

The method factorial(int n) of Guava's BigIntegerMath class is used to find the factorial of the given number. It returns n!, that is, the product of the first n positive integers. Syntax: public static BigInteger factorial(int n)

What is factorial recursion in Java?

The method fact() calculates the factorial of a number n. If n is less than or equal to 1, it returns 1. Otherwise it recursively calls itself and returns n * fact(n - 1). A code snippet which demonstrates this is as follows: public static long fact(long n) { if (n <= 1) return 1; else return n * fact(n - 1); }


3 Answers

Both are hopelessly naive. No serious application of factorial would use either one. I think both are inefficient for large n, and neither int nor long will suffice when the argument is large.

A better way would be to use a good gamma function implementation and memoization.

Here's an implementation from Robert Sedgewick.

Large values will require logarithms.

like image 175
duffymo Avatar answered Oct 25 '22 17:10

duffymo


Whenever you get an option to chose between recursion and iteration, always go for iteration because

1.Recursion involves creating and destroying stack frames, which has high costs.

2.Your stack can blow-up if you are using significantly large values.

So go for recursion only if you have some really tempting reasons.

like image 22
Ahmad Avatar answered Oct 25 '22 18:10

Ahmad


I was actually analyzing this problem by time factor. I've done 2 simple implementations:

Iterative:

private static BigInteger bigIterativeFactorial(int x) {
    BigInteger result = BigInteger.ONE;
    for (int i = x; i > 0; i--)
        result = result.multiply(BigInteger.valueOf(i));
    return result;
}

And Recursive:

public static BigInteger bigRecursiveFactorial(int x) {
    if (x == 0)
        return BigInteger.ONE;
    else
        return bigRecursiveFactorial(x - 1).multiply(BigInteger.valueOf(x));
}

Tests both running on single thread. It turns out that Iterative is slightly faster only with small arguments. When I put n bigger than 100 recursive solution was faster. My conclussion? You never can say that iterative solution is faster than recursive on JVM. (Still talking only about time)

If You're intrested, whole way I get this conclussion is HERE

If You're intrested in deeper understanding difference between this 2 approaches, I found really nice description on knowledge-cess.com

like image 36
Filip Kubala Avatar answered Oct 25 '22 19:10

Filip Kubala