Find numbers with K odd divisors in a given range | function to check if number is perfect square or not ; Function to return count of divisors of a number ; Note that this loop runs till square root ; If divisors are equal , count it only once ; Otherwise print both ; Function to calculate all divisors having exactly k divisors between a and b ; Initialize result ; calculate only for perfect square numbers ; check if number is perfect square or not ; total divisors of number equals to k or not ; Driver Code
< ? php function isPerfect ( $ n ) { $ s = sqrt ( $ n ) ; return ( $ s * $ s == $ n ) ; } function divisorsCount ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) + 1 ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ count += 1 ; else $ count += 2 ; } } return $ count ; } function kDivisors ( $ a , $ b , $ k ) { $ count = 0 ; for ( $ i = $ a ; $ i <= $ b ; $ i ++ ) { if ( isPerfect ( $ i ) ) if ( divisorsCount ( $ i ) == $ k ) $ count ++ ; } return $ count ; } $ a = 2 ; $ b = 49 ; $ k = 3 ; echo kDivisors ( $ a , $ b , $ k ) ; ? >