Even Perfect Number | Python3 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
import math NEW_LINE def check ( num ) : NEW_LINE INDENT root = ( int ) ( math . sqrt ( num ) ) NEW_LINE poww = ( int ) ( math . log ( root ) / math . log ( 2 ) ) NEW_LINE if ( num == ( int ) ( pow ( 2 , poww ) * ( pow ( 2 , poww + 1 ) - 1 ) ) ) : NEW_LINE INDENT if ( isPrime ( poww + 1 ) ) : NEW_LINE INDENT if ( isPrime ( ( int ) ( pow ( 2 , poww + 1 ) ) - 1 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return bool ( False ) NEW_LINE DEDENT elif ( n == 2 or n == 3 ) : NEW_LINE INDENT return bool ( True ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return bool ( False ) NEW_LINE DEDENT for i in range ( 5 , sqrt ( n + 1 ) + 1 , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return bool ( False ) NEW_LINE DEDENT DEDENT return bool ( True ) NEW_LINE DEDENT DEDENT num = 6 NEW_LINE check ( num ) NEW_LINE