0
Can any one tell me how to print large perfect number using big integer in Java.
1 Answer
0
import java.math.BigInteger;
public class PerfectNumber {
public static void main(String[] args) {
// The desired value of n
int n = 1000;
// Calculate the nth perfect number
BigInteger perfectNumber = calculatePerfectNumber(n);
// Print the result
System.out.println("The " + n + "th perfect number is: " + perfectNumber);
}
public static BigInteger calculatePerfectNumber(int n) {
// Initialize the variables used in the formula
BigInteger two = new BigInteger("2");
BigInteger p = two.pow(n - 1);
BigInteger q = two.pow(n).subtract(BigInteger.ONE);
// Calculate and return the perfect number
return p.multiply(q);
}
}