Count the numbers < N which have equal number of divisors as K | Function to return the count of the divisors of a number ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ; While i divides n ; This condition is to handle the case when n is a prime number > 2 ; Count the total elements that have divisors exactly equal to as that of k 's ; Exclude k from the result if it is smaller than n . ; Driver code
< ? php function countDivisors ( $ n ) { $ x = 0 ; $ ans = 1 ; while ( $ n % 2 == 0 ) { $ x ++ ; $ n = $ n / 2 ; } $ ans = $ ans * ( $ x + 1 ) ; for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { $ x = 0 ; while ( $ n % $ i == 0 ) { $ x ++ ; $ n = $ n / $ i ; } $ ans = $ ans * ( $ x + 1 ) ; } if ( $ n > 2 ) $ ans = $ ans * 2 ; return $ ans ; } function getTotalCount ( $ n , $ k ) { $ k_count = countDivisors ( $ k ) ; $ count = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ k_count == countDivisors ( $ i ) ) $ count ++ ; if ( $ k < $ n ) $ count = $ count - 1 ; return $ count ; } $ n = 500 ; $ k = 6 ; echo getTotalCount ( $ n , $ k ) ; #This  code is contributed by Sachin.. NEW_LINE ? >