Given a number K which represents the factorial of a number N, the task is to find the value of N.
Note: K < 1018
Input: K = 120
5! = 1 * 2 * 3 * 4 * 5 = 120
Input: K = 6
3! = 1 * 2 * 3 = 6
Approach: It is given that the value of N! is less than 1018. On observation we can see that this is true only for N <= 18. Therefore we can precompute values of factorials from 1 to 18 and store it in a Hash Table or Map. After precomputation, for every value of N!, the corresponding N is returned in constant time.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find (1^n + 2^n + 3^n + 4^n) mod 5 | Set 2
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Find the value of max(f(x)) - min(f(x)) for a given F(x)
- Find the value of f(n) / f(r) * f(n-r)
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find 2^(2^A) % B
- Find K such that |A - K| = |B - K|
- Find the value of ln(N!) using Recursion
- Find the other number when LCM and HCF given
- Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N
- Find F(n) when F(i) and F(j) of a sequence are given
- Find the value of N XOR'ed to itself K times
- Find any pair with given GCD and LCM
- Find the sum of series 3, 7, 13, 21, 31....
- Find x and y satisfying ax + by = n
- Given two numbers a and b find all x such that a % x = b
- Program to find value of 1^k + 2^k + 3^k + ... + n^k
- Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z
- Find (a^b)%m where 'b' is very large
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.