Hoax Number | Function to find distinct prime factors of given number n ; n is odd at this point , since it is no longer divisible by 2. So we can test only for the odd numbers , whether they are factors of n ; Check if i is prime factor ; This condition is to handle the case when n is a prime number greater than 2 ; Function to calculate sum of digits of distinct prime factors of given number n and sum of digits of number n and compare the sums obtained ; Distinct prime factors of n are being stored in vector pf ; If n is a prime number , it cannot be a hoax number ; Finding sum of digits of distinct prime factors of the number n ; Finding sum of digits in current prime factor pf [ i ] . ; Finding sum of digits of number n ; Comparing the two calculated sums ; Driver Code
< ? php function primeFactors ( $ n ) { $ res = array ( ) ; if ( $ n % 2 == 0 ) { while ( $ n % 2 == 0 ) $ n = ( int ) $ n / 2 ; array_push ( $ res , 2 ) ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { if ( $ n % $ i == 0 ) { while ( $ n % $ i == 0 ) $ n = ( int ) $ n / $ i ; array_push ( $ res , $ i ) ; } } if ( $ n > 2 ) array_push ( $ res , $ n ) ; return $ res ; } function isHoax ( $ n ) { $ pf = primeFactors ( $ n ) ; if ( $ pf [ 0 ] == $ n ) return false ; $ all_pf_sum = 0 ; for ( $ i = 0 ; $ i < count ( $ pf ) ; $ i ++ ) { $ pf_sum ; for ( $ pf_sum = 0 ; $ pf [ $ i ] > 0 ; $ pf_sum += $ pf [ $ i ] % 10 , $ pf [ $ i ] /= 10 ) ; $ all_pf_sum += $ pf_sum ; } for ( $ sum_n = 0 ; $ n > 0 ; $ sum_n += $ n % 10 , $ n /= 10 ) ; return $ sum_n == $ all_pf_sum ; } $ n = 84 ; if ( isHoax ( $ n ) ) echo ( " A ▁ Hoax ▁ Number STRNEWLINE " ) ; else echo ( " Not ▁ a ▁ Hoax ▁ Number STRNEWLINE " ) ; ? >