Program to find count of numbers having odd number of divisors in given range

Given two integers A and B. The task is to count how many numbers in the interval [ A, B ] have an odd number of divisors.

Examples:

Input : A = 1, B = 10
Output : 3

Input : A = 5, B = 15
Output : 1


Naive Approach :
The simple approach would be to iterate through all the numbers between range [A, B] and check if their number of divisors is odd.
Below is the implementation of the above idea :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find count of numbers having
// odd number of divisors in given range
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to count numbers having odd
// number of divisors in range [A, B]
int OddDivCount(int a, int b)
{
    // variable to odd divisor count
    int res = 0;
    // iterate from a to b and count their
    // number of divisors
    for (int i = a; i <= b; ++i) {
  
        // variable to divisor count
        int divCount = 0;
        for (int j = 1; j <= i; ++j) {
            if (i % j == 0) {
                ++divCount;
            }
        }
  
        // if count of divisor is odd
        // then increase res by 1
        if (divCount % 2) {
            ++res;
        }
    }
    return res;
}
  
// Driver code
int main()
{
    int a = 1, b = 10;
    cout << OddDivCount(a, b) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java program to find count of numbers having 
// odd number of divisors in given range 
  
import java.io.*;
  
class GFG {
    // Function to count numbers having odd 
// number of divisors in range [A, B] 
static int OddDivCount(int a, int b) 
    // variable to odd divisor count 
    int res = 0
    // iterate from a to b and count their 
    // number of divisors 
    for (int i = a; i <= b; ++i) { 
  
        // variable to divisor count 
        int divCount = 0
        for (int j = 1; j <= i; ++j) { 
            if (i % j == 0) { 
                ++divCount; 
            
        
  
        // if count of divisor is odd 
        // then increase res by 1 
        if ((divCount % 2) != 0) { 
            ++res; 
        
    
    return res; 
  
// Driver code 
      
    public static void main (String[] args) {
  
        int a = 1, b = 10
        System.out.println(OddDivCount(a, b)); 
          
    }
//This code is contributed by ajit. 
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find count 
# of numbers having odd number
# of divisors in given range
  
# Function to count numbers 
# having odd number of divisors 
# in range [A, B]
def OddDivCount(a, b):
  
    # variable to odd divisor count
    res = 0
      
    # iterate from a to b and count 
    # their number of divisors
    for i in range(a, b + 1) :
  
        # variable to divisor count
        divCount = 0
        for j in range(1, i + 1) :
            if (i % j == 0) :
                divCount += 1
  
        # if count of divisor is odd
        # then increase res by 1
        if (divCount % 2) :
            res += 1
    return res
  
# Driver code
if __name__ == "__main__":
    a = 1
    b = 10
    print(OddDivCount(a, b))
  
# This code is contributed 
# by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find count of numbers having
// odd number of divisors in given range
using System;
  
class Geeks {
      
// Function to count numbers having odd
// number of divisors in range [A, B]
static int OddDivCount(int a, int b)
{
    // variable to odd divisor count
    int res = 0;
    // iterate from a to b and count their
    // number of divisors
    for (int i = a; i <= b; ++i) {
  
        // variable to divisor count
        int divCount = 0;
        for (int j = 1; j <= i; ++j) {
            if (i % j == 0) {
                ++divCount;
            }
        }
  
        // if count of divisor is odd
        // then increase res by 1
        if ((divCount % 2) != 0) {
            ++res;
        }
    }
    return res;
}
  
// Driver code
public static void Main(String []args)
{
    int a = 1, b = 10;
    Console.WriteLine(OddDivCount(a, b));
}
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find count of
// numbers having odd number of
// divisors in given range
  
// Function to count numbers having odd
// number of divisors in range [A, B]
function OddDivCount($a, $b)
{
    // variable to odd divisor count
    $res = 0;
      
    // iterate from a to b and count 
    // their number of divisors
    for ($i = $a; $i <= $b; ++$i)
    {
  
        // variable to divisor count
        $divCount = 0;
        for ($j = 1; $j <= $i; ++$j
        {
            if ($i % $j == 0) 
            {
                ++$divCount;
            }
        }
  
        // if count of divisor is odd
        // then increase res by 1
        if ($divCount % 2) 
        {
            ++$res;
        }
    }
    return $res;
}
  
// Driver code
$a = 1;
$b = 10;
echo OddDivCount($a, $b) ;
  
// This code is contributed 
// by Shivi_Aggarwal 
?>

chevron_right



Output:

3

Please refer Number of elements with odd factors in given range for a better approach.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.