Check if a number is super perfect or not in Java
In this tutorial, you will learn about what is super-perfect number and the implementation in Java. But before that, you must know what is a super-perfect number.
What is a Super-perfect Number?
Super-perfect numbers are those number that satisfies the condition f2(n) = f(f(n)) = 2n, where f is a function that signifies the sum of all divisors of a number.
Java code to check if a number is super-perfect or not in Java
The following code can be used here to check it.
Step 1: First we will calculate the function f
package javaapplication15; public class JavaApplication15 { public static void main(String[] args) { int n = 16; int i,j,sum=1,sum1=1; System.out.print("f(n) = "+sum); for(i=2;i<=16;i++) { if(n%i==0) { System.out.print(" + "+i); sum = sum+i; } } System.out.println(" = " +sum);
Step 2: Then we will calculate f(f(n))
System.out.print("f(f(n)) = "+sum1); for(i=2;i<=sum;i++) { if(sum%i==0) { System.out.print(" + "+i); sum1 = sum1+i; } } System.out.println(" = " +sum1);
Step3: Now calculate 2*n
System.out.println("2 * n = "+2*n);
Step 4: Now check the condition if f(f(n)) = 2*n
if(n*2==sum1) System.out.println("Since f(f(n)) = 2*n therfore, the number is a superperfect number"); else System.out.println("Since f(f(n)) != 2*n therfore the number is not a superfect number"); } }
Output:-
f(n) = 1 + 2 + 4 + 8 + 16 = 31 f(f(n)) = 1 + 31 = 32 2 * n = 32 Since f(f(n)) = 2*n therfore, the number is a superperfect number
Also read: Java program to check if a number is a Carmichael number
Leave a Reply