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
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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll boost_hyperfactorial(ll num)
{
ll val = 1;
for ( int i = 1; i <= num; i++) {
val = val * pow (i,i);
}
return val;
}
int main()
{
int num = 5;
cout << boost_hyperfactorial(num);
return 0;
}
|
Java
class GFG
{
static long boost_hyperfactorial( long num)
{
long val = 1 ;
for ( int i = 1 ; i <= num; i++)
{
val = val * ( long )Math.pow(i, i);
}
return val;
}
public static void main(String args[])
{
int num = 5 ;
System.out.println(boost_hyperfactorial(num));
}
}
|
Python3
def boost_hyperfactorial(num):
val = 1 ;
for i in range ( 1 , num + 1 ):
val = val * pow (i, i);
return val;
num = 5 ;
print (boost_hyperfactorial(num));
|
C#
using System;
class GFG
{
static long boost_hyperfactorial( long num)
{
long val = 1;
for ( long i = 1; i <= num; i++)
{
val = val * ( long )Math.Pow(i, i);
}
return val;
}
public static void Main()
{
int num = 5;
Console.WriteLine(boost_hyperfactorial(num));
}
}
|
PHP
<?php
function boost_hyperfactorial( $num )
{
$val = 1;
for ( $i = 1; $i <= $num ; $i ++)
{
$val = $val * pow( $i , $i );
}
return $val ;
}
$num = 5;
echo boost_hyperfactorial( $num );
?>
|
Javascript
<script>
function boost_hyperfactorial(num)
{
let val = 1;
for (let i = 1; i <= num; i++)
{
val = val * Math.pow(i, i);
}
return val;
}
let num = 5;
document.write(boost_hyperfactorial(num));
</script>
|
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++
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;
int1024_t boost_hyperfactorial( int num)
{
int1024_t val = 1;
for ( int i = 1; i <= num; i++) {
for ( int j = 1; j <= i; j++) {
val *= i;
}
}
return val;
}
int main()
{
int num = 5;
cout << boost_hyperfactorial(num);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int boost_hyperfactorial( int num)
{
int val = 1 ;
for ( int i = 1 ; i <= num; i++) {
for ( int j = 1 ; j <= i; j++) {
val *= i;
}
}
return val;
}
public static void main (String[] args) {
int num = 5 ;
System.out.println( boost_hyperfactorial(num));
}
}
|
Python3
def boost_hyperfactorial(num):
val = 1 ;
for i in range ( 1 ,num + 1 ):
for j in range ( 1 ,i + 1 ):
val * = i;
return val;
num = 5 ;
print ( boost_hyperfactorial(num));
|
C#
using System;
class GFG
{
static int boost_hyperfactorial( int num)
{
int val = 1;
for ( int i = 1; i <= num; i++)
{
for ( int j = 1; j <= i; j++)
{
val *= i;
}
}
return val;
}
public static void Main ()
{
int num = 5;
Console.WriteLine(boost_hyperfactorial(num));
}
}
|
PHP
<?php
function boost_hyperfactorial( $num )
{
$val = 1;
for ( $i = 1; $i <= $num ; $i ++)
{
for ( $j = 1; $j <= $i ; $j ++)
{
$val *= $i ;
}
}
return $val ;
}
$num = 5;
echo boost_hyperfactorial( $num );
?>
|
Javascript
<script>
function boost_hyperfactorial(num)
{
var val = 1;
for ( var i = 1; i <= num; i++) {
for ( var j = 1; j <= i; j++) {
val *= i;
}
}
return val;
}
var num = 5;
document.write( boost_hyperfactorial(num));
</script>
|
Time Complexity: O(N2), where N is the given number.
Auxiliary Space: O(1)
Last Updated :
02 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...