Find numbers with K odd divisors in a given range | Utility function to check if number is perfect square or not ; Utility 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
function isPerfect ( n ) { var s = parseInt ( ( Math . sqrt ( n ) ) ) ; return ( s * s == n ) ; } function divisorsCount ( n ) { var count = 0 ; for ( var i = 1 ; i <= parseInt ( Math . sqrt ( n ) ) + 1 ; i ++ ) { if ( n % i == 0 ) { if ( parseInt ( n / i ) == i ) count += 1 ; else count += 2 ; } } return count ; } function kDivisors ( a , b , k ) { var count = 0 ; for ( var i = a ; i <= b ; i ++ ) { if ( isPerfect ( i ) ) { if ( divisorsCount ( i ) == k ) { count ++ ; } } } return count ; } var a = 2 , b = 49 , k = 3 ; document . write ( kDivisors ( a , b , k ) ) ;