Numbers with Odd Factors in Given Range (original) (raw)

Last Updated : 7 Jun, 2026

Given an integer **n, count the numbers havingan odd numberof factors from 1 to n (inclusive).

**Examples :

**Input: n = 5
**Output: 2
**Explanation: The numbers from 1 to 5 with an odd number of factors are 1 and 4.
1 has 1 factor: [1]
4 has 3 factors: [1, 2, 4]
Hence, the count is 2.

**Input: n = 1
**Output: 1
**Explanation: 1 has exactly one factor, [1], which is odd. Therefore, the count is 1

Try It Yourselfredirect icon

Table of Content

[Naive Approach] Check Factors for Every Number - O(n√n) Time O(1) Space

The idea is to iterate through all numbers from 1 to n and count their factors by checking divisors up to their square root. If a number has an odd number of factors, increment the result. Finally, return the total count of such numbers.

C++ `

#include using namespace std;

int count(int n) { int res = 0;

// Check every number from 1 to n
for (int i = 1; i <= n; i++)
{
    int factors = 0;

    // Count factors of i
    for (int j = 1; j * j <= i; j++)
    {
        if (i % j == 0)
        {

            // Count the factor j
            factors++;

            // Count the paired factor if distinct
            if (j != i / j)
            {
                factors++;
            }
        }
    }

    // If number of factors is odd, increment result
    if (factors % 2 == 1)
    {
        res++;
    }
}

return res;

}

// Driver Code int main() { int n = 5;

cout << count(n);

return 0;

}

Java

import java.io.*;

public class GfG { public static int count(int n) { int res = 0;

    // Check every number from 1 to n
    for (int i = 1; i <= n; i++) {
        int factors = 0;

        // Count factors of i
        for (int j = 1; j * j <= i; j++) {
            if (i % j == 0) {

                // Count the factor j
                factors++;

                // Count the paired factor if distinct
                if (j != i / j) {
                    factors++;
                }
            }
        }

        // If number of factors is odd, increment result
        if (factors % 2 == 1) {
            res++;
        }
    }

    return res;
}

// Driver Code
public static void main(String[] args)
{
    int n = 5;

    System.out.println(count(n));
}

}

Python

def count(n): res = 0

# Check every number from 1 to n
for i in range(1, n + 1):
    factors = 0

    # Count factors of i
    for j in range(1, int(i ** 0.5) + 1):
        if i % j == 0:

            # Count the factor j
            factors += 1

            # Count the paired factor if distinct
            if j != i // j:
                factors += 1

    # If number of factors is odd, increment result
    if factors % 2 == 1:
        res += 1

return res

Driver Code

if name == "main": n = 5 print(count(n))

C#

using System;

public class GfG { public static int count(int n) { int res = 0;

    // Check every number from 1 to n
    for (int i = 1; i <= n; i++) {
        int factors = 0;

        // Count factors of i
        for (int j = 1; j * j <= i; j++) {
            if (i % j == 0) {

                // Count the factor j
                factors++;

                // Count the paired factor if distinct
                if (j != i / j) {
                    factors++;
                }
            }
        }

        // If number of factors is odd, increment result
        if (factors % 2 == 1) {
            res++;
        }
    }

    return res;
}

// Driver Code
public static void Main()
{
    int n = 5;

    Console.WriteLine(count(n));
}

}

JavaScript

function count(n) { let res = 0;

// Check every number from 1 to n
for (let i = 1; i <= n; i++) {
    let factors = 0;

    // Count factors of i
    for (let j = 1; j * j <= i; j++) {
        if (i % j === 0) {

            // Count the factor j
            factors++;

            // Count the paired factor if distinct
            if (j !== i / j) {
                factors++;
            }
        }
    }

    // If number of factors is odd, increment result
    if (factors % 2 === 1) {
        res++;
    }
}

return res;

}

// Driver Code let n = 5;

console.log(count(n));

`

**Time Complexity: O(n√n)
**Auxiliary Space: O(1)

[Expected Approach] Count Perfect Squares - O(1) Time O(1) Space

The idea is based on the observation that only perfect squares have an odd number of factors. Therefore, the required count is simply the number of perfect squares in the range [1, n], which is equal to the integer part of √n.

Factors occur in pairs (d, n/d), so a non-perfect square has an even number of factors. A perfect square has one unpaired factor, √n, giving it an odd number of factors. Hence, only perfect squares have an odd number of factors, and the answer is ⌊√n⌋.

#include #include using namespace std;

int count(int n) {

// Count of perfect squares from 1 to n
int res = (int)sqrt(n);

return res;

}

// Driver Code int main() { int n = 5;

cout << count(n);

return 0;

}

Java

import java.lang.Math;

public class GfG {

// Count of perfect squares from 1 to n
public static int count(int n)
{
    int res = (int)Math.sqrt(n);
    return res;
}

// Driver Code
public static void main(String[] args)
{
    int n = 5;
    System.out.println(count(n));
}

}

Python

import math

Count of perfect squares from 1 to n

def count(n): res = int(math.sqrt(n)) return res

Driver Code

if name == "main": n = 5 print(count(n))

C#

using System;

public class GfG { // Function to count the number of set bits in the given // number. public int count(int n) { int res = (int)(Math.Sqrt(n)); return res; }

// Driver Code
public static void Main()
{
    int n = 5;

    GfG obj = new GfG();
    Console.WriteLine(obj.count(n));
}

}

JavaScript

// Count of perfect squares from 1 to n function count(n) { var res = Math.floor(Math.sqrt(n)); return res; }

// Driver Code var n = 5; console.log(count(n));

`

**Time Complexity: O(1)
**Auxiliary Space: O(1)