Check whether a given Number is Power | PHP program to find whether a number is power - isolated or not ; for 2 as prime factor ; for odd prime factor ; calculate product of powers and prime factors ; check result for power - isolation ; driver code
< ? php function checkIfPowerIsolated ( $ num ) { $ input = $ num ; $ count = 0 ; $ factor = array ( ) ; if ( $ num % 2 == 0 ) { while ( $ num % 2 == 0 ) { ++ $ count ; $ num /= 2 ; } $ factor [ 2 ] = $ count ; } for ( $ i = 3 ; $ i * $ i <= $ num ; $ i += 2 ) { $ count = 0 ; while ( $ num % $ i == 0 ) { ++ $ count ; $ num /= $ i ; } if ( $ count ) $ factor [ $ i ] = $ count ; } if ( $ num > 1 ) $ factor [ $ num ] = 1 ; $ product = 1 ; foreach ( $ factor as $ primefactor = > $ power ) { $ product = $ product * $ primefactor * $ power ; } if ( $ product == $ input ) print_r ( " Power - isolated ▁ Integer STRNEWLINE " ) ; else print_r ( " Not ▁ a ▁ Power - isolated ▁ Integer STRNEWLINE " ) ; } checkIfPowerIsolated ( 12 ) ; checkIfPowerIsolated ( 18 ) ; checkIfPowerIsolated ( 35 ) ; ? >