# Hyperfactorial of a number

Given a number, the task is to find the hyperfactorial of a number.

The result of multiplying a given number of consecutive integers from 1 to the given number, each raised to its own power is called hyperfactorial of a number.

`H(n)= 1 ^ 1 * 2 ^ 2 * 3 ^ 3 * . . . . . * n ^ n`

Examples:

Input : 2
Output : 4

Input : 4
Output : 27648
H(4) = 1^1 * 2^2 * 3^3 * 4^4 = 27648

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A naive approach is two use two loops, one to find the summation of i^i and the other to find i^i. But the time complexity will be O(n2).

An efficient approach is to use the inbuilt pow() function or the O(log n) method to find i^i and then add it.

Below is the implementation of the above approach.

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

 ` `

Output:

```86400000
```

Time-complexity:O(N * log N)

Since hyper-factorials of number can be huge, hence the numbers will overflow. We can use boost libraries in C++ or BigInteger in Java to store the hyper-factorial of a number 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 ` `int1024_t boost_hyperfactorial(``int` `num) ` `{ ` `    ``// initialise the val to 1 ` `    ``int1024_t val = 1; ` `    ``for` `(``int` `i = 1; i <= num; i++) { ` `        ``for` `(``int` `j = 1; j <= i; j++) { ` `            ``// 1^1*2^2*3^3.... ` `            ``val *= 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 using boost libraries  ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `// function to calculate the value of hyperfactorial ` `static` `int` `boost_hyperfactorial(``int` `num) ` `{ ` `    ``// initialise the val to 1 ` `    ``int` `val = ``1``; ` `    ``for` `(``int` `i = ``1``; i <= num; i++) { ` `        ``for` `(``int` `j = ``1``; j <= i; j++) { ` `            ``// 1^1*2^2*3^3.... ` `            ``val *= 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 using boost libraries  ` ` `  `# 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``):  ` `        ``for` `j ``in` `range``(``1``,i``+``1``):  ` `             `  `            ``# 1^1*2^2*3^3....  ` `            ``val ``*``=` `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 boost libraries  ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `// function to calculate the ` `// value of hyperfactorial ` `static` `int` `boost_hyperfactorial(``int` `num) ` `{ ` `    ``// initialise the val to 1 ` `    ``int` `val = 1; ` `    ``for` `(``int` `i = 1; i <= num; i++)  ` `    ``{ ` `        ``for` `(``int` `j = 1; j <= i; j++) ` `        ``{ ` `            ``// 1^1*2^2*3^3.... ` `            ``val *= 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

 ` `

Output:

```86400000
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.