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 : 4Input : 4Â
Output : 27648Â
H(4) = 1^1 * 2^2 * 3^3 * 4^4 = 27648Â
A naive approach is using 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 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 <bits/stdc++.h> Â
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
<script> Â
// 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 Â
</script> |
86400000
Â
Time complexity:O(N * log N)Â
Auxiliary Space: O(1)
Since hyper-factorials of numbers 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 <bits/stdc++.h> #include <boost/multiprecision/cpp_int.hpp> Â
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 Â
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 Â
# 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
<?php // PHP program to find the hyperfactorial // of a number using boost libraries Â
// function to calculate the value // of hyperfactorial function boost_hyperfactorial( $num ) {     // initialise the val to 1     $val = 1;     for ( $i = 1; $i <= $num ; $i ++)     {         for ( $j = 1; $j <= $i ; $j ++)         {             // 1^1*2^2*3^3....             $val *= $i ;         }     }          // returns the hyperfactorial     // of a number     return $val ; } Â
// Driver code $num = 5; echo boost_hyperfactorial( $num ); Â
// This code is contributed // by Mukul Singh ?> |
Javascript
<script> Â
// Javascript program to find the hyperfactorial // of a number using boost libraries Â
// function to calculate the value of hyperfactorial function boost_hyperfactorial(num) {     // initialise the val to 1     var val = 1;     for ( var i = 1; i <= num; i++) {         for ( var j = 1; j <= i; j++) {             // 1^1*2^2*3^3....             val *= i;         }     }     // returns the hyperfactorial of a number     return val; } Â
// Driver code var num = 5; document.write( boost_hyperfactorial(num)); Â
Â
</script> |
86400000
Â
Time Complexity: O(N2), where N is the given number.
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!