Javatpoint Logo
Javatpoint Logo

Number of digits in N factorial to the power N in Java

In Java, finding the number of digits in N factorial raised to the power of N is a fascinating puzzle. As N increases, the resulting number can become large, requiring careful handling. The task involves counting how many digits are in the final result and calls for clever solutions in Java programming.

The total number of digits in the factorial of N raised to the power N, or (N!)^N, must be determined given a positive integer N.

Example 1:

Input: 4

Output: 6

Explanations: (4!)^4 = (24)^4 = 331776. Total number of digits in 331776 is 6.

Example 2:

Input: 2

Output: 1

Explanations: (2!)^2 = (2)^2 = 4. Total number of digits in 4 is 1.

Example 3:

Input: 5

Output: 11

Explanation: (5!)^5 = (120)^5 = 24883200000. Total number of digits in 24883200000 is 11.

Approach: Brute Force Approach

In the brute force approach, you'd compute N! and then multiply it by itself N times (N!)^N.

Algorithm

Step 1: Import the 'BigInteger' class from the 'java.math' package in your code.

Step 2: Create a 'FactorialDigits' class and a 'countDigits' method for determining the number of digits in a 'BigInteger'.

Step 3: Begin the `main` method for program execution.

Step 4: Set the desired value of `N` (integer). Initialize a `BigInteger` variable named `factorialResult` to `1` to store the result of (N!).

Step 5: Start a `for` loop, iterating from `1` to `N`. Inside the loop, multiply the current `factorialResult` by the current value of `i' to calculate (N!).

Step 6: Using the 'pow' method, raise the computed (N!) to the power of 'N', saving the result in a 'BigInteger' variable named'result'.

Step 7: Call the 'countDigits' method, providing the'result' as an argument, to find the number of digits in the final result.

Step 8: Print the number of digits in the result, as well as a description of the calculation.ss

Implementation:

Filename: FactorialDigits.java

Output:

Number of digits in 4!^4: 6

Time Complexity: The time complexity of the above code is (O(N^2)), and the time complexity is determined by the nested loop used to calculate the factorial (N!).

Auxiliary Space: The time complexity of the above code is (O(N)), and the auxiliary space complexity is linear and directly proportional to the input size (N!).

Approach: Logarithmic Sum Approach

Using the logarithmic properties of numbers, you can simplify (N!)^N into N×log10(N!). Taking the common logarithm of (N!)^N breaks down the computation into more manageable parts.

Further Simplification:

  • N! can be expressed as the product of individual numbers from 1 to N.
  • By using logarithmic properties, you can simplify N×log10(N!) to N×[log10(1)+log10(2)+log10(3)+…+log10(N)].

Computation:

  • The sum of logarithms can be computed in linear time (O(N)).

Benefits:

  • The approach avoids the need to compute large factorials directly, which can lead to performance and memory issues for large N.
  • Instead, it calculates the sum of logarithms, which is more manageable.

Algorithm:

Step 1: Accept the value of (N) (replace `n` with the desired value).

Step 2: Initialize a variable `sumOfLogarithms` to 0.

Step 3:. Iterate from 1 to (N): Add Math.log10(i) to `sumOfLogarithms` for each iteration.

Step 4: Calculate the final result by multiplying `sumOfLogarithms` by (N) and adding 1 (for rounding up).

Step 5: Print the calculated result, indicating the number of digits in ((N!)^N).

Implementation:

Filename: NumberOfDigitsInFactorialPower.java

Output:

Number of digits in (4!)^4 = 6

Time complexity: The code has an O(N * log(N)) time complexity, where N is the input number. Because the for loop iterates from 1 to N, and the Math.log10() method has an O(log(N)) complexity.

Auxiliary space: The auxiliary space of the above code is O(1) since the only variable used is a double variable, which takes constant space.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA