📜  N的指数阶乘

📅  最后修改于: 2022-05-13 01:56:06.463000             🧑  作者: Mango

N的指数阶乘

给定一个正整数N ,任务是打印N的指数阶乘。由于输出可能非常大,请打印答案模数1000000007

例子:

方法:可以根据以下观察解决给定的问题:

请按照以下步骤解决问题:

  • 初始化一个变量res1以存储N的指数阶乘。
  • 使用变量i在范围[2, N]上进行迭代,并在每次迭代中将res更新为res = i res %1000000007。
  • 最后,完成上述步骤后,打印res中得到的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find exponential factorial
// of a given number
int ExpoFactorial(int N)
{
   
    // Stores the exponential factor of N
    int res = 1;
    int mod = 1000000007;
 
    // Iterate over the range [2, N]
    for (int i = 2; i < N + 1; i++)
       
        // Update res
        res = (int)pow(i, res) % mod;
 
    // Return res
    return res;
}
 
// Driver Code
int main()
{
    // Input
    int N = 4;
   
    // Function call
    cout << (ExpoFactorial(N));
   
   // This code is contributed by Potta Lokesh
    return 0;
}
 
// This code is contributed by lokesh potta


Java
// Java program for the above approach
class GFG{
     
// Function to find exponential factorial
// of a given number
static int ExpoFactorial(int N)
{
     
    // Stores the exponential factor of N
    int res = 1;
    int mod = 1000000007;
 
    // Iterate over the range [2, N]
    for(int i = 2; i < N + 1; i++)
     
        // Update res
        res = (int)Math.pow(i, res) % mod;
 
    // Return res
    return res;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Input
    int N = 4;
 
    // Function call
    System.out.println((ExpoFactorial(N)));
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to find exponential factorial
# of a given number
 
 
def ExpoFactorial(N):
    # Stores the exponential factor of N
    res = 1
    mod = (int)(1000000007)
 
    # Iterate over the range [2, N]
    for i in range(2, N + 1):
        # Update res
        res = pow(i, res, mod)
 
    # Return res
    return res
 
 
# Driver Code
 
# Input
N = 4
# Function call
print(ExpoFactorial(N))


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find exponential factorial
// of a given number
static int ExpoFactorial(int N)
{
     
    // Stores the exponential factor of N
    int res = 1;
    int mod = 1000000007;
 
    // Iterate over the range [2, N]
    for(int i = 2; i < N + 1; i++)
     
        // Update res
        res = (int)Math.Pow(i, res) % mod;
 
    // Return res
    return res;
}
 
// Driver Code
public static void Main()
{
    // Input
    int N = 4;
 
    // Function call
    Console.Write(ExpoFactorial(N));
 
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出
262144

时间复杂度: O(N*log(N))
辅助空间: O(1)