给定一个数字,我们的任务是找到该数字的超级阶乘。
将 1 到给定数字的连续整数相乘的结果,其中每个整数都以其自身为幂,这就是所谓的数字 超级阶乘。
H(n)= 1 ^ 1 * 2 ^ 2 * 3 ^ 3 * . . . . . * n ^ n
示例:
> 输入: 2
> 输出: 4
>
>
>
>
>
> 输入: 4
> 输出: 27648
> H(4) = 1^1 2^2 3^3 * 4^4 = 27648
一种朴素的方法是使用两个循环,一个用于计算 i^i 的总和,另一个用于计算 i^i。但这种方法的时间复杂度将是 O(n2)。
一种高效的方法是使用内置的 pow() 函数或 O(log n) 方法 来计算 i^i,然后将其相加。
下面是上述方法的实现。
C++
“/// C++ program to find the hyperfactorial
// of a number
#include
using namespace std;
#define ll long long
// function to calculate the value of hyperfactorial
ll boost_hyperfactorial(ll num)
{
// initialise the val to 1
ll val = 1;
for (int i = 1; i <= num; i++) {
val = val * pow(i,i);
}
// returns the hyperfactorial of a number
return val;
}
// Driver code
int main()
{
int num = 5;
cout << boost_hyperfactorial(num);
return 0;
}
`
Java
`
// Java program to find the
// hyperfactorial of a number
// function to calculate the
// value of hyperfactorial
class GFG
{
static long boost_hyperfactorial(long num)
{
// initialise the val to 1
long val = 1;
for (int i = 1; i <= num; i++)
{
val = val * (long)Math.pow(i, i);
}
// returns the hyperfactorial
// of a number
return val;
}
// Driver code
public static void main(String args[])
{
int num = 5;
System.out.println(boost_hyperfactorial(num));
}
}
// This code is contributed
// by chandan_jnu
`
Python3
`
# Python3 program to find the
目录
hyperfactorial of a number
function to calculate the
value of hyperfactorial
def boost_hyperfactorial(num):
# initialise the
# val to 1
val = 1;
for i in range(1, num + 1):
val = val * pow(i, i);
# returns the hyperfactorial
# of a number
return val;
Driver code
num = 5;
print(boost_hyperfactorial(num));
This code is contributed
by mits
`
C#
`
// C# program to find the
// hyperfactorial of a number
using System;
class GFG
{
// function to calculate the
// value of hyperfactorial
static long boost_hyperfactorial(long num)
{
// initialise the val to 1
long val = 1;
for (long i = 1; i <= num; i++)
{
val = val * (long)Math.Pow(i, i);
}
// returns the hyperfactorial
// of a number
return val;
}
// Driver code
public static void Main()
{
int num = 5;
Console.WriteLine(boost_hyperfactorial(num));
}
}
// This code is contributed
// by chandan_jnu
`
PHP
`
<?php
// PHP program to find the
// hyperfactorial of a number
// function to calculate the
// value of hyperfactorial
function boost_hyperfactorial($num)
{
// initialise the
// val to 1
$val = 1;
for ($i = 1; $i <= $num; $i++)
{
$val = $val * pow($i, $i);
}
// returns the hyperfactorial
// of a number
return $val;
}
// Driver code
$num = 5;
echo boost_hyperfactorial($num);
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>
`
JavaScript
`
// Javascript program to find the
// hyperfactorial of a number
// function to calculate the
// value of hyperfactorial
function boost_hyperfactorial(num)
{
// initialise the
// val to 1
let val = 1;
for (let i = 1; i <= num; i++)
{
val = val * Math.pow(i, i);
}
// returns the hyperfactorial
// of a number
return val;
}
// Driver code
let num = 5;
document.write(boost_hyperfactorial(num));
// This code is contributed
// by gfgking
`
**输出:**
86400000
**时间复杂度:** O(N * log N)
**辅助空间:** O(1)
由于数字的超级阶乘可能非常巨大,数字将会溢出。我们可以使用 [C++ 中的 boost 库](https://www.geeksforgeeks.org/cpp/advanced-c-boost-library/) 或 [Java 中的 BigInteger](https://www.geeksforgeeks.org/java/biginteger-class-in-java/) 来存储数字 N 的超级阶乘。
C++
`
// C++ program to find the hyperfactorial
// of a number using boost libraries
#include
#include
using namespace boost::multiprecision;
using namespace std;
// function to calculate the value of hyperfactorial
int1024t boosthyperfactorial(int num)
{
// initialise the val to 1
int1024_t val = 1;
for (int i = 1; i <= num; i++) {