Given a range [** n**,

**], find the number of elements that have odd number of factors in the given range (**

*m***and**

*n***inclusive).**

*m*

Examples :

Examples :

Input : n = 5, m = 100 Output : 8 The numbers with odd factors are 9, 16, 25, 36, 49, 64, 81 and 100 Input : n = 8, m = 65 Output : 6 Input : n = 10, m = 23500 Output : 150

A **Simple Solution** is to loop through all numbers starting from ** n**. For every number, check if it has an even number of factors. If it has an even number of factors then increment count of such numbers and finally print the number of such elements. To find all divisors of a natural number efficiently, refer All divisors of a natural number

An **Efficient Solution** is to observe the pattern. Only those numbers, which are **perfect Squares** have an odd number of factors. Let us analyze this pattern through an example.

For example, 9 has odd number of factors, 1, 3 and 9. 16 also has odd number of factors, 1, 2, 4, 8, 16. The reason for this is, for numbers other than perfect squares, all factors are in the form of pairs, but for perfect squares, one factor is single and makes the total as odd.

**How to find number of perfect squares in a range?**

The answer is difference between square root of **m** and **n-1** (**not n**)

There is a little caveat. As both **n** and **m** are inclusive, if **n** is a perfect square, we will get an answer which is less than one the actual answer. To understand this, consider range [4, 36]. Answer is 5 i.e., numbers 4, 9, 16, 25 and 36.

But if we do (36**0.5) – (4**0.5) we get 4. So to avoid this semantic error, we take **n-1**.

## C++

`// C++ program to count number of odd squares ` `// in given range [n, m] ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `countOddSquares(` `int` `n, ` `int` `m) ` `{ ` ` ` `return` `(` `int` `)` `pow` `(m,0.5) - (` `int` `)` `pow` `(n-1,0.5); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5, m = 100; ` ` ` `cout << ` `"Count is "` `<< countOddSquares(n, m); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number of odd squares ` `// in given range [n, m] ` ` ` `import` `java.io.*; ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `int` `countOddSquares(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `return` `(` `int` `)Math.pow((` `double` `)m,` `0.5` `) - (` `int` `)Math.pow((` `double` `)n-` `1` `,` `0.5` `); ` ` ` `} ` ` ` `// Driver code for above functions ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `, m = ` `100` `; ` ` ` `System.out.print(` `"Count is "` `+ countOddSquares(n, m)); ` ` ` `} ` `} ` `// Mohit Gupta_OMG <(o_0)> ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to count number of odd squares ` `# in given range [n, m] ` ` ` `def` `countOddSquares(n, m): ` ` ` `return` `int` `(m` `*` `*` `0.5` `) ` `-` `int` `((n` `-` `1` `)` `*` `*` `0.5` `) ` ` ` `# Driver code ` `n ` `=` `5` `m ` `=` `100` `print` `(` `"Count is"` `, countOddSquares(n, m)) ` ` ` `# Mohit Gupta_OMG <0_o> ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number of odd ` `// squares in given range [n, m] ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to count odd squares ` ` ` `public` `static` `int` `countOddSquares(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `return` `(` `int` `)Math.Pow((` `double` `)m, 0.5) - ` ` ` `(` `int` `)Math.Pow((` `double` `)n - 1, 0.5); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 5, m = 100; ` ` ` `Console.Write(` `"Count is "` `+ countOddSquares(n, m)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nitin Mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count ` `// number of odd squares ` `// in given range [n, m] ` ` ` `function` `countOddSquares(` `$n` `, ` `$m` `) ` `{ ` `return` `pow(` `$m` `, 0.5) - ` ` ` `pow(` `$n` `- 1, 0.5); ` `} ` ` ` `// Driver code ` `$n` `= 5; ` `$m` `= 100; ` `echo` `"Count is "` `, ` ` ` `countOddSquares(` `$n` `, ` `$m` `); ` ` ` `// This code is contributed ` `// by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

Output :

Output :

Count is 8

**
Time Complexity :** O(1)

This article is contributed by **Divyanshu Bansal**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find number of factors of N when location of its two factors whose product is N is given
- Maximum number of prime factors a number can have with exactly x factors
- Number of elements with even factors in the given range
- Check if a number exists having exactly N factors and K prime factors
- Sum of all odd factors of numbers in the range [l, r]
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Queries on sum of odd number digit sums of all the factors of a number
- Find sum of odd factors of a number
- Expressing a number as sum of consecutive | Set 2 (Using odd factors)
- Check whether count of odd and even factors of a number are equal
- Find and Count total factors of co-prime A or B in a given range 1 to N
- Find elements in a given range having at least one odd divisor
- Count of elements having odd number of divisors in index range [L, R] for Q queries
- Average of odd numbers till a given odd number
- Number of factors of very large number N modulo M where M is any prime number
- Program to find count of numbers having odd number of divisors in given range
- K-Primes (Numbers with k prime factors) in a range
- Count numbers from range whose prime factors are only 2 and 3
- Count numbers in a range having GCD of powers of prime factors equal to 1