Program to find count of numbers having odd number of divisors in given range
Last Updated :
11 Jul, 2025
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++
// 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;
}
Java
// 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.
}
Python3
# 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
C#
// 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));
}
}
PHP
<?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
?>
JavaScript
<script>
// Javascript 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
let res = 0;
// Iterate from a to b and count their
// number of divisors
for(let i = a; i <= b; ++i)
{
// Variable to divisor count
let divCount = 0;
for(let 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
let a = 1, b = 10;
document.write(OddDivCount(a, b));
// This code is contributed by suresh07
</script>
Time complexity: O(n2)
Auxiliary Space: O(1)
Better Approach:
A number can be represented by the product of its prime factors with appropriate powers. Those powers can be used to get the number of factors an integer has. If the number is num and it can be represented as (ap1) * (bp2) * (cp3)
Then the count of factors of num are (p1 + 1) * (p2 + 1) * (p3 + 1)
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the count
// of divisors of a number
int divisor(int a)
{
int div = 1, count = 0;
for (int i = 2; i <= sqrt(a); i++) {
// Count the powers of the current
// prime i which divides a
while (a % i == 0) {
count++;
a = a / i;
}
// Update the count of divisors
div = div * (count + 1);
// Reset the count
count = 0;
}
// If the remaining a is prime then a^1
// will be one of its prime factors
if (a > 1) {
div = div * (2);
}
return div;
}
// Function to count numbers having odd
// number of divisors in range [A, B]
int OddDivCount(int a, int b)
{
// To store the count of elements
// having odd number of divisors
int res = 0;
// Iterate from a to b and find the
// count of their divisors
for (int i = a; i <= b; ++i) {
// To store the count of divisors of i
int divCount = divisor(i);
// If the divisor count of i is odd
if (divCount % 2) {
++res;
}
}
return res;
}
// Driver code
int main()
{
int a = 1, b = 10;
cout << OddDivCount(a, b);
return 0;
}
// This code is contributed by jrolofmeister
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to return the count
// of divisors of a number
static int divisor(int a)
{
int div = 1, count = 0;
for (int i = 2; i <= Math.sqrt(a); i++)
{
// Count the powers of the current
// prime i which divides a
while (a % i == 0)
{
count++;
a = a / i;
}
// Update the count of divisors
div = div * (count + 1);
// Reset the count
count = 0;
}
// If the remaining a is prime then a^1
// will be one of its prime factors
if (a > 1)
{
div = div * (2);
}
return div;
}
// Function to count numbers having odd
// number of divisors in range [A, B]
static int OddDivCount(int a, int b)
{
// To store the count of elements
// having odd number of divisors
int res = 0;
// Iterate from a to b and find the
// count of their divisors
for (int i = a; i <= b; ++i)
{
// To store the count of divisors of i
int divCount = divisor(i);
// If the divisor count of i is odd
if (divCount % 2 == 1)
{
++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 PrinciRaj1992
Python3
# Python3 implementation of the approach
# Function to return the count
# of divisors of a number
def divisor(a):
div = 1;
count = 0;
for i in range(2, int(pow(a, 1 / 2)) + 1):
# Count the powers of the current
# prime i which divides a
while (a % i == 0):
count += 1;
a = a / i;
# Update the count of divisors
div = div * (count + 1);
# Reset the count
count = 0;
# If the remaining a is prime then a^1
# will be one of its prime factors
if (a > 1):
div = div * (2);
return div;
# Function to count numbers having odd
# number of divisors in range [A, B]
def OddDivCount(a, b):
# To store the count of elements
# having odd number of divisors
res = 0;
# Iterate from a to b and find the
# count of their divisors
for i in range(a, b + 1):
# To store the count of divisors of i
divCount = divisor(i);
# If the divisor count of i is odd
if (divCount % 2):
res += 1;
return res;
# Driver code
if __name__ == '__main__':
a, b = 1, 10;
print(OddDivCount(a, b));
# This code is contributed by PrinciRaj1992
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the count
// of divisors of a number
static int divisor(int a)
{
int div = 1, count = 0;
for (int i = 2;
i <= Math.Sqrt(a); i++)
{
// Count the powers of the current
// prime i which divides a
while (a % i == 0)
{
count++;
a = a / i;
}
// Update the count of divisors
div = div * (count + 1);
// Reset the count
count = 0;
}
// If the remaining a is prime then a^1
// will be one of its prime factors
if (a > 1)
{
div = div * (2);
}
return div;
}
// Function to count numbers having odd
// number of divisors in range [A, B]
static int OddDivCount(int a, int b)
{
// To store the count of elements
// having odd number of divisors
int res = 0;
// Iterate from a to b and find the
// count of their divisors
for (int i = a; i <= b; ++i)
{
// To store the count of divisors of i
int divCount = divisor(i);
// If the divisor count of i is odd
if (divCount % 2 == 1)
{
++res;
}
}
return res;
}
// Driver code
public static void Main(String[] args)
{
int a = 1, b = 10;
Console.WriteLine(OddDivCount(a, b));
}
}
// This code is contributed by Princi Singh
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the count
// of divisors of a number
function divisor(a)
{
let div = 1, count = 0;
for (let i = 2; i <= Math.sqrt(a); i++)
{
// Count the powers of the current
// prime i which divides a
while (a % i == 0)
{
count++;
a = parseInt(a / i, 10);
}
// Update the count of divisors
div = div * (count + 1);
// Reset the count
count = 0;
}
// If the remaining a is prime then a^1
// will be one of its prime factors
if (a > 1)
{
div = div * (2);
}
return div;
}
// Function to count numbers having odd
// number of divisors in range [A, B]
function OddDivCount(a, b)
{
// To store the count of elements
// having odd number of divisors
let res = 0;
// Iterate from a to b and find the
// count of their divisors
for (let i = a; i <= b; ++i)
{
// To store the count of divisors of i
let divCount = divisor(i);
// If the divisor count of i is odd
if (divCount % 2 == 1)
{
++res;
}
}
return res;
}
let a = 1, b = 10;
document.write(OddDivCount(a, b));
</script>
Time complexity: O(n * logn)
Auxiliary Space: O(1)
Please refer this article for an O(1) approach.
Similar Reads
Count of elements having odd number of divisors in index range [L, R] for Q queries Given an array arr[] of N positive integers and the number of queries Q, each query contains two numbers L and R. The task is to count the number of elements in the array having an odd number of divisors from index L to R. Examples: Input: arr[] = [2, 4, 5, 6, 9], Q = 3, Query[][] = { {0, 2}, {1, 3}
12 min read
Find the number of divisors of all numbers in the range [1, n] Given an integer N. The task is to find the number of divisors of all the numbers in the range [1, N]. Examples: Input: N = 5 Output: 1 2 2 3 2 divisors(1) = 1 divisors(2) = 1 and 2 divisors(3) = 1 and 3 divisors(4) = 1, 2 and 4 divisors(5) = 1 and 5 Input: N = 10 Output: 1 2 2 3 2 4 2 4 3 4 Approac
5 min read
Find numbers with K odd divisors in a given range Given two numbers a and b, and a number k which is odd. The task is to find all the numbers between a and b (both inclusive) having exactly k divisors.Examples: Input : a = 2, b = 49, k = 3 Output: 4 // Between 2 and 49 there are four numbers // with three divisors // 4 (Divisors 1, 2, 4), 9 (Diviso
8 min read
Count of nodes having odd divisors in the given subtree for Q queries Given a N-ary Tree and Q queries where each query contains a node of the N-ary tree, the task is to count the number of nodes that have an odd number of divisors in the subtree for Q queries. Examples: Input: Output: 1 3 0 1 Explanation: Query 1: In the subtree rooted at node 100, there is only one
8 min read
Count of integers in a range which have even number of odd digits and odd number of even digits Given a range [L, R], the task is to count the numbers which have even number of odd digits and odd number of even digits. For example, 8 has 1 even digit and 0 odd digit - Satisfies the condition since 1 is odd and 0 is even.545 has 1 even digit and 2 odd digits - Satisfies the condition since 1 is
11 min read
Count elements in the given range which have maximum number of divisors Given two numbers X and Y. The task is to find the number of elements in the range [X,Y] both inclusive, that have the maximum number of divisors. Examples: Input: X = 2, Y = 9 Output: 2 6, 8 are numbers with the maximum number of divisors. Input: X = 1, Y = 10 Output: 3 6, 8, 10 are numbers with th
15+ min read