Determine whether a given number is a Hyperperfect Number | Python3 program to check whether a given number is k - hyperperfect ; Function to find the sum of all proper divisors ( excluding 1 and N ) ; Iterate only until sqrt N as we are going to generate pairs to produce divisors ; As divisors occur in pairs , we can take the values i and N / i as long as i divides N ; Function to check whether the given number is prime ; Base and corner cases ; Since integers can be represented as some 6 * k + y where y >= 0 , we can eliminate all integers that can be expressed in this form ; Start from 5 as this is the next prime number ; Returns true if N is a K - Hyperperfect number . Else returns false . ; Condition from the definition of hyperperfect ; Driver code ; First two statements test against the condition N = 1 + K * ( sum ( proper divisors ) )
import math NEW_LINE def divisorSum ( N , K ) : NEW_LINE INDENT Sum = 0 NEW_LINE for i in range ( 2 , math . ceil ( math . sqrt ( N ) ) ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT Sum += ( i + int ( N / i ) ) NEW_LINE DEDENT DEDENT return Sum NEW_LINE DEDENT def isPrime ( n ) : NEW_LINE INDENT if ( n == 1 or n == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def isHyperPerfect ( N , K ) : NEW_LINE INDENT Sum = divisorSum ( N , K ) NEW_LINE if ( ( 1 + K * ( Sum ) ) == N ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT N1 = 1570153 NEW_LINE K1 = 12 NEW_LINE N2 = 321 NEW_LINE K2 = 3 NEW_LINE if ( isHyperPerfect ( N1 , K1 ) ) : NEW_LINE INDENT print ( N1 , " ▁ is ▁ " , K1 , " - HyperPerfect " , sep = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( N1 , " ▁ is ▁ not ▁ " , K1 , " - HyperPerfect " , sep = " " ) NEW_LINE DEDENT if ( isHyperPerfect ( N2 , K2 ) ) : NEW_LINE INDENT print ( N2 , " ▁ is ▁ " , K2 , " - HyperPerfect " , sep = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( N2 , " ▁ is ▁ not ▁ " , K2 , " - HyperPerfect " , sep = " " ) NEW_LINE DEDENT