Find N from the value of N! - GeeksforGeeks

16 Aug, 2021
Given a number K which represents the factorial of a number N, the task is to find the value of N. Note: K < 1018Examples:  
Input: K = 120 Output: 5 Explanation: 5! = 1 * 2 * 3 * 4 * 5 = 120Input: K = 6 Output: 3 Explanation: 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:  
C++
Java
Python3
C#
Javascript
// C++ program to find a number such that// the factorial of that number is given #include "bits/stdc++.h"#define ll long long intusing namespace std; // Map to precompute and store the// factorials of the numbersmap<ll, ll> m; // Function to precompute factorialint 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 codeint main(){    // Precomputing the factorials    precompute();     int K = 120;    cout << m[K] << endl;     K = 6;    cout << m[K] << endl;     return 0;}
// Java program to find a number such that// the factorial of that number is givenimport java.util.*; class GFG{  // Map to precompute and store the// factorials of the numbersstatic Map<Integer, Integer> m = new HashMap<Integer, Integer>();  // Function to precompute factorialstatic 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 codepublic 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
# Python3 program to find a number such that# the factorial of that number is given # Map to precompute and store the# factorials of the numbersm = {}; # Function to precompute factorialdef 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 codeif __name__ == "__main__" :     # Precomputing the factorials    precompute();     K = 120;    print(m[K]);     K = 6;    print(m[K]) ; # This code is contributed by AnkitRai01
// C# program to find a number such that// the factorial of that number is givenusing System;using System.Collections.Generic; class GFG{   // Map to precompute and store the// factorials of the numbersstatic Dictionary<int, int> m = new Dictionary<int, int>();   // Function to precompute factorialstatic 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 codepublic 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
<script>// Javascript implementation of the// above approach // Map to precompute and store the// factorials of the numbersvar m = {};   // Function to precompute factorialfunction precompute(){       var fact = 1;    for (var i = 1; i <= 18; i++) {           // Calculating the factorial for        // each i and storing in a map        fact = fact * i;        m[fact] = i;    }} // Driver codeprecompute();var K = 120;document.write(m[K] + "<br>"); K = 6;document.write(m[K]);  // This code is contributed by Shivanisingh</script>
5
3
 
ankthon
29AjayKumar
shivanisinghss2110
anikakapoor
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Program to multiply two matrices
Fizz Buzz Implementation
Modular multiplicative inverse
Python Dictionary
Arrays in C/C++
Reverse a string in Java
Inheritance in C++
Interfaces in Java