Given a positive integer N, the task is to print the Exponential factorial of N. Since the output can be very large, print the answer modulus 1000000007.
Examples:
Input: N = 4
Output: 262144Input: N = 3
Output: 9
Approach: The given problem can be solved based on the following observations:
The exponential factorial is defined by the recurrence relation:
a_n=n^{a_{n-1}} .
Follow the steps below to solve the problem:
- Initialize a variable say res as 1 to store the exponential factorial of N.
- Iterate over the range [2, N] using the variable i and in each iteration update the res as res = ires%1000000007.
- Finally, after completing the above step, print the answer obtained in res.
Below is the implementation of the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
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 program for the above approach
import java.io.*;
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 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# 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.
<script>
// JavaScript program for the above approach
// Function to find exponential factorial
// of a given number
function ExpoFactorial(N) {
// Stores the exponential factor of N
let res = 1;
let mod = 1000000007;
// Iterate over the range [2, N]
for (let i = 2; i < N + 1; i++)
// Update res
res = Math.pow(i, res) % mod;
// Return res
return res;
}
// Driver Code
// Input
let N = 4;
// Function call
document.write((ExpoFactorial(N)));
// This code is contributed by _saurabh_jaiswal
</script>
Output
262144
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)