Find numbers with K odd divisors in a given range | C ++ program to count numbers with k odd divisors in a 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 ; calculate only for perfect square numbers ; check if number is perfect square or not ; total divisors of number equals to k or not ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfect ( int n ) { int s = sqrt ( n ) ; return ( s * s == n ) ; } int divisorsCount ( int n ) { int count = 0 ; for ( int i = 1 ; i <= sqrt ( n ) + 1 ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) count += 1 ; else count += 2 ; } } return count ; } int kDivisors ( int a , int b , int k ) { for ( int i = a ; i <= b ; i ++ ) { if ( isPerfect ( i ) ) if ( divisors ( i ) == k ) count ++ ; } return count ; } int main ( ) { int a = 2 , b = 49 , k = 3 ; cout << kDivisors ( a , b , k ) ; return 0 ; }