Find the next Factorial greater than N

Given a number N (≤ 1018), the task is to find the next factorial number greater than N.

Examples:

Input: N = 24
Output: 120
Explanation:
As 4! = 24. So the next number which factorial and greater than 24 is 5!, which is 120

Input: N = 150
Output: 720
Explanation:
As 5! = 120. So the next number which factorial and greater than 150 is 6!, which is 720.

Approach:



  1. Precompute the factorial of all number upto 20 in an array as 20! > 1018.
  2. Traverse the factorial array and find the value which is just greater than N as the required next factorial number.

Below is the implementation of above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
  
#include "bits/stdc++.h"
using namespace std;
  
// Array that stores the factorial
// till 20
long long fact[21];
  
// Function to pre-compute
// the factorial till 20
void preCompute()
{
  
    // Precomputing factorials
    fact[0] = 1;
  
    for (int i = 1; i < 18; i++)
        fact[i] = (fact[i - 1] * i);
}
  
// Function to return the next
// factorial number greater than N
void nextFactorial(int N)
{
    // Traverse the factorial array
    for (int i = 0; i < 21; i++) {
  
// Find the next just greater
// factorial than N
        if (N < fact[i]) {
  
            cout << fact[i];
            break;
        }
    }
}
  
// Driver Code
int main()
{
    // Function to precalculate
    // the factorial till 20
    preCompute();
  
    int N = 120;
  
    // Function call
    nextFactorial(N);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
class GFG {
      
// Array that stores the factorial
// till 20
final static int fact[] = new int[21];
  
    // Function to pre-compute
    // the factorial till 20
    static void preCompute()
    {
      
        // Precomputing factorials
        fact[0] = 1;
      
        for (int i = 1; i < 18; i++)
            fact[i] = (fact[i - 1] * i);
    }
      
    // Function to return the next
    // factorial number greater than N
    static void nextFactorial(int N)
    {
        // Traverse the factorial array
        for (int i = 0; i < 21; i++) {
      
            // Find the next just greater
            // factorial than N
            if (N < fact[i]) {
      
                System.out.println(fact[i]);
                break;
            }
        }
    }
      
    // Driver Code
    public static void main (String[] args)
    {
        // Function to precalculate
        // the factorial till 20
        preCompute();
      
        int N = 120;
      
        // Function call
        nextFactorial(N);
    }
      
}
  
// This code is contributed by AnkitRai01
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach 
  
# Array that stores the factorial 
# till 20 
fact = [0] * 21 
  
# Function to pre-compute 
# the factorial till 20 
def preCompute(): 
  
    # Precomputing factorials 
    fact[0] = 1 
  
    for i in range(1, 18): 
        fact[i] = (fact[i - 1] * i)
  
# Function to return the next 
# factorial number greater than N 
def nextFactorial(N): 
   
    # Traverse the factorial array 
    for i in range(21): 
  
# Find the next just greater 
# factorial than N 
        if N < fact[i]: 
  
            print(fact[i]) 
            break 
  
# Driver Code 
# Function to precalculate 
# the factorial till 20 
preCompute() 
  
N = 120 
  
# Function call 
nextFactorial(N)
  
# This code is contributed by divyamohan123
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
  
class GFG {
      
    // Array that stores the factorial
    // till 20
    static int []fact = new int[21];
  
    // Function to pre-compute
    // the factorial till 20
    static void preCompute()
    {
      
        // Precomputing factorials
        fact[0] = 1;
      
        for (int i = 1; i < 18; i++)
            fact[i] = (fact[i - 1] * i);
    }
      
    // Function to return the next
    // factorial number greater than N
    static void nextFactorial(int N)
    {
        // Traverse the factorial array
        for (int i = 0; i < 21; i++) {
      
            // Find the next just greater
            // factorial than N
            if (N < fact[i]) {
      
                Console.WriteLine(fact[i]);
                break;
            }
        }
    }
      
    // Driver Code
    public static void Main (string[] args)
    {
        // Function to precalculate
        // the factorial till 20
        preCompute();
      
        int N = 120;
      
        // Function call
        nextFactorial(N);
    }
      
}
  
// This code is contributed by AnkitRai01
chevron_right

Output:
720

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.




Article Tags :
Practice Tags :