Determine whether a given number is a Hyperperfect Number | 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 ) )
< ? php function divisorSum ( $ N , $ K ) { $ sum = 0 ; for ( $ i = 2 ; $ i <= ceil ( sqrt ( $ N ) ) ; $ i ++ ) if ( $ N % $ i == 0 ) $ sum += ( $ i + $ N / $ i ) ; return $ sum ; } function isPrime ( $ n ) { if ( $ n == 1 $ n == 0 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } function isHyperPerfect ( $ N , $ K ) { $ sum = divisorSum ( $ N , $ K ) ; if ( ( 1 + $ K * ( $ sum ) ) == $ N ) return true ; else return false ; } $ N1 = 1570153 ; $ K1 = 12 ; $ N2 = 321 ; $ K2 = 3 ; if ( isHyperPerfect ( $ N1 , $ K1 ) ) echo $ N1 , " ▁ is ▁ " , $ K1 , " - HyperPerfect " , " STRNEWLINE " ; else echo $ N1 , " ▁ is ▁ not ▁ " , $ K1 , " - HyperPerfect " , " STRNEWLINE " ; if ( isHyperPerfect ( $ N2 , $ K2 ) ) echo $ N2 , " ▁ is ▁ " , K2 , " - HyperPerfect " , " STRNEWLINE " ; else echo $ N2 , " ▁ is ▁ not ▁ " , $ K2 , " - HyperPerfect " , " STRNEWLINE " ; ? >