Find the last two digits of the Factorial of a given Number in Java

Finding the last two digits of the Factorial of a given number in Java is a common mathematical computation. The task involves calculating the Factorial and extracting only the last two digits of the result. Java provides various approaches to achieve this.

Consider an integer Num; the task is to find the last two digits of the Factorial of a number.

Example 1:

Input: N = 3

Output: 06

Example 2:

Input: N = 8

Output: 40

Example 3:

Input: N = 10

Output: 00

Approach: Modular Arithmetic Approach

It efficiently computes the factorial modulo 100, ensuring that only the last two digits are retained in the result. The code provides a simple solution for determining these last two digits.

Algorithm

Step 1: Start by defining an integer variable num and set it to the desired input value.

Step 2: Define an integer variable lastTwoDigits to store the last two digits of the factorial. Initialize it to 0.

Step 3: Check if the value of num is greater than or equal to 10. If it is, then set lastTwoDigits to 0 because the factorial of numbers greater than or equal to 10 will always end in '00' in decimal notation.

Step 4: If num is less than 10, call the findLastTwoDigitsOfFactorial method with num as an argument to calculate the last two digits of the factorial.

Step 5: Inside the findLastTwoDigitsOfFactorial method:

  1. Initialize an integer variable factorial to 1.
  2. Use a for loop to calculate the factorial of n by iterating from 2 to n.
  3. In each iteration, multiply the current value of factorial by i and take the modulo 100 to ensure that only the last two digits are kept.
  4. Return the calculated factorial value, which contains the last two digits of the factorial of n.

Step 6: Print the input num and the output lastTwoDigits to display the result.

Implementation

Filename: LastTwoDigitsOfFactorial.java

Output:

Input: Num = 8
Output: 20

Time Complexity: The time complexity of the code is O(n) due to the calculation of the Factorial using a loop that runs from 2 to n.

Auxiliary Space: The auxiliary space complexity is O(1) as the space used by the code remains constant, regardless of the input value n.

Approach: Using dynamic programming

Using a dynamic programming approach to find the last two digits of the Factorial of a given number involves building a table or an array to store intermediate results efficiently.

Algorithm

Step 1: Start by defining an integer variable n and set it to the desired input value.

Step 2: Create an integer variable lastTwoDigits to store the last two digits of the factorial result.

Step 3: Check if n is less than or equal to 1. If true, return 1 because the factorial of 0 and 1 is always 1, and there are no additional digits to calculate.

Step 4: Create an integer array factorialTable of size n + 1 to store the factorial values. Initialize the first two elements of the array as 1 since the factorial of 0 and 1 is 1.

Step 5: Use a for loop to calculate and store the factorial values from 2 to n. In each iteration: a. Multiply the current element factorialTable[i - 1] by i. b. Take the modulo 100 of the result to keep only the last two digits. c. Store the calculated value in factorialTable[i].

Step 6: After the loop completes, the factorialTable array will contain the last two digits of n! at the factorialTable[n].

Step 7: Return the value stored at factorialTable[n], which represents the last two digits of the factorial of n.

Step 8: Print the input n and the output lastTwoDigits to display the result.

Implementation

Filename: LastTwoDigitsOfFactorial.java

Output:

Last two digits of 8! are: 20

Time Complexity: The main loop in the calculateLastTwoDigits method runs from i = 2 to i <= n, iterating n - 1 times. Therefore, the time complexity of the code is O(n).

Auxiliary Space: The auxiliary space complexity of the provided code is O(n). It is primarily due to the space used to store the factorialTable array, which has a size of n + 1 to store factorial values for numbers from 0 to n.