Find Hyperfactorial in Java



What is hyperfactorial?

The hyperfactorial of a number is the product of numbers from 1 to the given number where each number is raised to the power of itself. For example, the hyperfactorial of 4 will be ?

= 1^1 * 2^2 * 3^3 * 4^4 
= 1*4*27*256
= 27648

So we can say that the formula for finding hyperfactorial is ?

H(n)=1^1 * 2^2 *3^3 * 4^4 * 5^5 * . . . . . . . . . * n^n

In this article, we will learn different approaches to find the hyperfactorial of a number, from the brute force method to the most efficient method.

Why we use BigInteger for hyperfactorial?

In all the approaches, we will be using BigInteger to represent hyperfactorial value instead of the integer or long datatype. This is because the value of hyperfactorials can be so huge that integers or longs cannot handle them and can cause overflow. Some important BigInteger functions are mentioned below.

  • One ? represents 1 in BigInteger
  • valueOf(n) ? Converts an integer n to a BigInteger
  • multiply(BigInteger val) ? Multiplies two BigInteger values
  • pow(int exponent) ? calculates the power of a Biginteger number
  • toBigInteger() ? Converts BigDecimal to BigInteger

Brute force method

In this approach we will use the brute force method for calculating the hyperfactorial of a number. In the program, we will iterate from 1 to n, calculate the power of itself using a nested for loop, multiply each value together, and return the result.

Algorithm

Step 1 ? Declare a variable named 'result' to store the value of hyperfactorial and initialize it as 1 using the 'BigInteger.One' function.

Step 2 ? Construct a loop that iterates from i to n.

Step 3 ? Construct an inner loop that calculates i^i by multiplying i with itself i times and stores its result in a variable named 'power'.

Step 4 ? Now multiply the 'power' to the variable 'result' after each iteration of the outer loop.

Step 5 ? Return the final result.

Implementation Code

Below is the Java implementation to find the hyperfactorial using a nested loop.

import java.math.BigInteger;
public class Hyperfactorial{
   public static BigInteger hyperfactorial(int n) {
      BigInteger result = BigInteger.ONE;
      for (int i = 1; i <= n; i++) {
         BigInteger power = BigInteger.ONE;
         for (int j = 0; j < i; j++) { 
            power = power.multiply(BigInteger.valueOf(i));
         }
         result = result.multiply(power);
      }
      return result;
   }
   public static void main(String[] args) {
      int n = 4;
      System.out.println("Hyperfactorial of " + n + " is: " + hyperfactorial(n));
   } 
}

Output

Hyperfactorial of 4 is: 27648

Using BigInteger.pow() method

In this approach we use BigInteger.pow() to calculate the power of itself (i*i) instead of using an inner loop, which we used in the previous approach. This increases the efficiency of the code.

Implementation Code

Below is the Java implementation using BigInteger.pow() for improved efficiency.

import java.math.BigInteger;
public class Hyperfactorialpow{
   public static BigInteger hyperfactorial(int n) {
      BigInteger result = BigInteger.ONE;
      for (int i = 1; i <= n; i++) {
         result = result.multiply(BigInteger.valueOf(i).pow(i));
      }
      return result;
   } public static void main(String[] args) {
      int n = 5;
      System.out.println("Hyperfactorial of " + n + " is: " + hyperfactorial(n));
   }
}

Output

Hyperfactorial of 5 is: 86400000

Using Recursion

This approach uses recursion to find the hyperfactorial of a number. In this method we create a recursive function that returns 1 if the number is 1, otherwise recursively calls itself with the previous number (number - 1) and multiplies the result with the number raised to the power of itself. This recursive approach is relatively less efficient than the pow() approach but faster than the brute force approach.

Implementation Code

Below is the Java implementation using recursion to find the hyperfactorial of a number.

import java.math.BigInteger;
public class HyperfactorialRecursion {
   public static BigInteger hyperfactorial(int num) {
      if (num == 1) {
         return BigInteger.ONE;
      }
      return hyperfactorial(num - 1).multiply(BigInteger.valueOf(num).pow(num));
   }  
   public static void main(String[] args) {
      int num = 5;
      System.out.println("Hyperfactorial of " + num + " is: " + hyperfactorial(num));
   } 
}

Output

Hyperfactorial of 5 is: 86400000

Using Logarithm

We can find hyperfactorials of a number using logarithms, and it is the most efficient way of finding hyperfactorials. In this approach we use logarithmic properties for calculation instead of directly multiplying large numbers.

Algorithm

Step 1: Initialize variable logsum to store the sum of logarithms.

Step 2: Create a loop that iterates from i= 1 to n, and inside the loop calculate I * log(i) and add it to logsum.

Step 3: Use Math.exp(logsum) to calculate the exponential of logsum.

Step 4: Now convert BigDecimal to BigInteger.

Step 5: Return the result.

Implementation Code

Below is the Java implementation using logarithms for better efficiency.

import java.math.BigDecimal;
import java.math.BigInteger;
public class HyperfactorialLogarithm{
   public static BigInteger hyperfactorial(int n) {
      double logsum = 0.0;
      for (int i = 1; i <= n; i++) {
         logsum += i * Math.log(i);
      }
      BigDecimal result = BigDecimal.valueOf(Math.exp(logsum));
      return result.toBigInteger();
   }
   public static void main(String[] args) {
      int n = 5;
      System.out.println("Hyperfactorial of " + n + " is: " + hyperfactorial(n));
   } 
}

Output

Hyperfactorial of 5 is: 86400000
Updated on: 2025-02-27T11:18:17+05:30

49 Views

Kickstart Your Career

Get certified by completing the course

Get Started