Given a number **K** which represents the factorial of a number **N**, the task is to find the value of **N**.

**Note:** K < 10^{18}

**Examples:**

Input:K = 120

Output:5

Explanation:

5! = 1 * 2 * 3 * 4 * 5 = 120

Input:K = 6

Output:3

Explanation:

3! = 1 * 2 * 3 = 6

**Approach:** It is given that the value of N! is less than 10^{18}. 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:

## C++

`// C++ program to find a number such that ` `// the factorial of that number is given ` ` ` `#include "bits/stdc++.h" ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// Map to precompute and store the ` `// factorials of the numbers ` `map<ll, ll> m; ` ` ` `// Function to precompute factorial ` `int` `precompute() ` `{ ` ` ` ` ` `ll fact = 1; ` ` ` `for` `(ll i = 1; i <= 18; i++) { ` ` ` ` ` `// Calculating the factorial for ` ` ` `// each i and storing in a map ` ` ` `fact = fact * i; ` ` ` `m[fact] = i; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Precomputing the factorials ` ` ` `precompute(); ` ` ` ` ` `int` `K = 120; ` ` ` `cout << m[K] << endl; ` ` ` ` ` `K = 6; ` ` ` `cout << m[K] << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find a number such that ` `// the factorial of that number is given ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Map to precompute and store the ` `// factorials of the numbers ` `static` `Map<Integer, Integer> m = ` `new` `HashMap<Integer, Integer>(); ` ` ` `// Function to precompute factorial ` `static` `void` `precompute() ` `{ ` ` ` ` ` `int` `fact = ` `1` `; ` ` ` `for` `(` `int` `i = ` `1` `; i <= ` `18` `; i++) { ` ` ` ` ` `// Calculating the factorial for ` ` ` `// each i and storing in a map ` ` ` `fact = fact * i; ` ` ` `m.put(fact, i); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// Precomputing the factorials ` ` ` `precompute(); ` ` ` ` ` `int` `K = ` `120` `; ` ` ` `System.out.print(m.get(K) +` `"\n"` `); ` ` ` ` ` `K = ` `6` `; ` ` ` `System.out.print(m.get(K) +` `"\n"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find a number such that ` `# the factorial of that number is given ` ` ` `# Map to precompute and store the ` `# factorials of the numbers ` `m ` `=` `{}; ` ` ` `# Function to precompute factorial ` `def` `precompute() : ` ` ` ` ` `fact ` `=` `1` `; ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `19` `) : ` ` ` ` ` `# Calculating the factorial for ` ` ` `# each i and storing in a map ` ` ` `fact ` `=` `fact ` `*` `i; ` ` ` `m[fact] ` `=` `i; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# Precomputing the factorials ` ` ` `precompute(); ` ` ` ` ` `K ` `=` `120` `; ` ` ` `print` `(m[K]); ` ` ` ` ` `K ` `=` `6` `; ` ` ` `print` `(m[K]) ; ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find a number such that ` `// the factorial of that number is given ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG{ ` ` ` `// Map to precompute and store the ` `// factorials of the numbers ` `static` `Dictionary<` `int` `, ` `int` `> m = ` `new` `Dictionary<` `int` `, ` `int` `>(); ` ` ` `// Function to precompute factorial ` `static` `void` `precompute() ` `{ ` ` ` ` ` `int` `fact = 1; ` ` ` `for` `(` `int` `i = 1; i <= 18; i++) { ` ` ` ` ` `// Calculating the factorial for ` ` ` `// each i and storing in a map ` ` ` `fact = fact * i; ` ` ` `m.Add(fact, i); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `// Precomputing the factorials ` ` ` `precompute(); ` ` ` ` ` `int` `K = 120; ` ` ` `Console.Write(m[K] +` `"\n"` `); ` ` ` ` ` `K = 6; ` ` ` `Console.Write(m[K] +` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

5 3

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.

## Recommended Posts:

- Find the maximum possible value of the minimum value of modified array
- Find length of period in decimal value of 1/n
- Find minimum number of coins that make a given value
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
- Find minimum value to assign all array elements so that array product becomes greater
- Find minimum value of y for the given x values in Q queries from all the given set of lines
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find value of y mod (2 raised to power x)
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Find the value of N XOR'ed to itself K times
- Program to find greater value between a^n and b^n
- Find minimum number of Log value needed to calculate Log upto N
- Program to Find the value of cos(nΘ)
- Find the missing value from the given equation a + b = c
- Program to find the value of tan(nΘ)
- Program to find the value of sin(nΘ)
- Find the value of the function Y = (X^6 + X^2 + 9894845) % 971
- Find the value of max(f(x)) - min(f(x)) for a given F(x)
- Find maximum value of x such that n! % (k^x) = 0
- Find number from given list for which value of the function is closest to A

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.