📜  查找下一个大于N的阶乘

📅  最后修改于: 2021-04-22 06:35:03             🧑  作者: Mango

给定数字N (≤10 18 ),任务是找到下一个大于N的阶乘数。
例子:

方法:

  1. 将数组中最多20的阶乘分解因子预先计算为20! > 10 18
  2. 遍历阶乘数组并找到刚好大于N的值作为所需的下一个阶乘数。

下面是上述方法的实现:

C++
// 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;
}


Java
// 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


Python3
# 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


C#
// 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


Javascript


输出:
720