Even Perfect Number | Java program for the above approach ; Function to check for perfect number ; Find a number close to 2 ^ q - 1 ; Calculate q - 1 ; Condition of perfect number ; Check whether q is prime or not ; Check whether 2 ^ q - 1 is a prime number or not ; Function to check for prime number ; Check whether it is equal to 2 or 3 ; Check if it can be divided by 2 and 3 then it is not prime number ; Check whether the given number be divide by other prime numbers ; Driver code
class GFG { private static void check ( long num ) { long root = ( long ) Math . sqrt ( num ) ; long pow = ( long ) ( Math . log ( root ) / Math . log ( 2 ) ) ; if ( num == ( long ) ( Math . pow ( 2 , pow ) * ( Math . pow ( 2 , pow + 1 ) - 1 ) ) ) { if ( isPrime ( pow + 1 ) ) { if ( isPrime ( ( long ) Math . pow ( 2 , pow + 1 ) - 1 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } else System . out . println ( " No " ) ; } else System . out . println ( " No " ) ; } public static boolean isPrime ( long n ) { if ( n <= 1 ) return false ; else if ( n == 2 n == 3 ) return true ; else { if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( long i = 5 ; i <= Math . sqrt ( n ) ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; } return true ; } } public static void main ( String args [ ] ) { long num = 6 ; check ( num ) ; } }