Factorial Program in Java

Factorial Program in Java: The factorial of a non-negative integer n is the product of all positive integers less than or equal to n. The formula for calculating the factorial of a number n is:

Factorial of n is denoted by n!. For example:

Here, 4! is pronounced as "4 factorial." It is also called "4 bang" or "4 shriek."

The factorial is normally used in Combinations and Permutations (mathematics).

There are many ways to write a factorial program in Java. Let's examine all of them.

  • Factorial Program using loop
  • Factorial Program using recursion
  • Factorial Program using Dynamic Programing

Factorial Program Using loop in Java

Let's see the factorial Program using a loop in Java.

FactorialExample.java

Output:

Factorial of 5 is: 120

Time Complexity: O(n)

The iterative approach implements a single loop that starts from 1 and ends with n. Each iteration takes a constant amount of time (to multiply).

Space Complexity: O(1)

The iterative approach allocates only a fixed additional space, no matter of the size of the input. This does not depend on the last number in a series (n).

Factorial Program Using Recursion in Java

Let's see the factorial program in Java using recursion.

FactorialExample2.java

Output:

Factorial of 4 is: 24

Time Complexity: O(n)

This, too, has a time complexity of O(n), with the worst-case scenario involving n recursive calls in a constant amount of time.

Space Complexity: O(n)

The space complexity of the recursive operations is O(n) as for every function call, memory is allocated for the call stack. In the worst situation, the size of the recursion tree is n, which means that the call stack uses O(n) space.

Factorial Program Using Dynamic Programming in Java

1. Tabulation (Bottom-up) Approach:

Tabulation approach involves filling up a table (usually an array) iteratively to store the results of subproblems, starting from the smallest subproblems and building up towards the final solution.

Let's see the factorial program in Java using Tabulation.

Factorial.java

Output:

Factorial of 5 is: 120

Time Complexity: O(n)

The tabulation approach cycles through numbers 1 to n just once to compute their factorials. Consequently, the time complexity is O(n) which is a linear relationship with the input size(n).

Space Complexity: O(n)

The space complexity is also O(n), as the size of the array is n+1 for the storage of the factorial values.

Memoization (Top-down DP) Approach:

Memoization approach involves storing the results of already computed subproblems to avoid redundant computations during recursion.

Let's see the factorial program in Java using Memoization.

Factorial.java

Output:

Factorial of 5 is: 120

Time Complexity: O(n)

However, though it seems the time complexity of O(n^2) comes from the recursive calls, memoization makes sure that every value gets computed only once. In other words, the complexity of the algorithm is linear time.

Space Complexity: O(n)

The time complexity is O(n) since it is a recursive algorithm that uses the call stack. More importantly, the value of the memoization cache (HashMap) is O(n) in space complexity since n entries with each constant size can be stored.


Java Factorial Program MCQ

1. What is the primary advantage of using BigInteger over int or long for computing the factorial of a large number in Java?

  1. BigInteger is faster than int and long.
  2. BigInteger provides a built-in factorial method.
  3. BigInteger can handle arbitrarily large numbers without overflow.
  4. BigInteger uses less memory compared to int and long.
 

Answer: c)

Explanation: BigInteger can handle arbitrarily large numbers without overflow, which is a significant advantage when calculating the factorial of large numbers that would otherwise exceed the storage capacity of int or long.


2. Considering computational efficiency, which approach is generally more memory efficient for calculating factorials in Java?

  1. Iterative approach
  2. Recursive approach
  3. Using BigInteger
  4. Using memoization
 

Answer: a)

Explanation: The iterative approach is generally more memory efficient than the recursive approach because it does not involve the overhead of multiple function calls and the associated call stack usage.


3. Which of the following could cause a stack overflow error when computing the factorial of a large number in Java?

  1. Using an iterative approach
  2. Using a recursive approach
  3. Using BigInteger
  4. Using a for loop
 

Answer: b)

Explanation: A stack overflow error can occur when using a recursive approach to compute the factorial of a large number because each recursive call consumes stack space, and a large number of calls can exceed the stack's limit.


4. What is the time complexity of both the iterative and recursive approaches to computing the factorial of a number in Java?

  1. O(1)
  2. O(log n)
  3. O(n)
  4. O(n^2)
 

Answer: c)

Explanation: Both the iterative and recursive approaches to computing the factorial of a number have a time complexity of O(n), as they both involve a single loop or series of recursive calls that iterate or recurse n times


5. When computing the factorial of a number using recursion, how can we optimize the performance and avoid redundant calculations?

  1. By using a larger stack size
  2. By using memoization
  3. By increasing the JVM heap size
  4. By using multithreading
 

Answer: b)

Explanation: Memoization is a technique that can optimize the performance of a recursive function by storing the results of expensive function calls and reusing them when the same inputs occur again, thus avoiding redundant calculations.


Next TopicJava Programs