Can any one tell me how to print large perfect number using big integer in Java. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can any one tell me how to print large perfect number using big integer in Java.

9th Jan 2023, 4:37 PM
Srinivas vedullapalli
Srinivas vedullapalli - avatar
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); } }
9th Jan 2023, 4:42 PM
Mwanzi Victor
Mwanzi Victor - avatar