Given a range [n, m], the task is to find the number of elements that have even number of factors in the given range (n and m inclusive).

**Examples :**

Input:n = 5, m = 20Output:14 The numbers with even factors are 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20.Input:n = 5, m = 100Output: 88

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 find the numbers with odd number of factors i.e only the perfect squares have odd number of factors, so all numbers other than perfect squares will have even number of factors. So, find the count of perfect squares in the range and subtract from the total numbers i.e. **m-n+1** .

Below is the implementation of the above approach:

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to count the perfect squares ` `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 "`
` ` `<< (m - n + 1) - countOddSquares(n, m); `
` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java implementation of the above approach ` `import` `java.io.*; `
` ` `class` `GFG `
`{ ` ` ` `// Function to count the perfect squares ` `static` `int` `countOddSquares(` `int` `n, ` `int` `m) `
`{ ` ` ` `return` `(` `int` `)Math.pow(m, ` `0.5` `) - `
` ` `(` `int` `)Math.pow(n - ` `1` `, ` `0.5` `); `
`} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) `
`{ ` ` ` `int` `n = ` `5` `, m = ` `100` `; `
` ` `System.out.println(` `"Count is "` `+ ((m - n + ` `1` `) `
` ` `- countOddSquares(n, m))); `
`} ` `} ` ` ` `// This code is contributed by ajit.. ` |

*chevron_right*

*filter_none*

`# Python3 implementation of the ` `# above approach ` ` ` `# Function to count the perfect squares ` `def` `countOddSquares(n, m) : `
` ` `return` `(` `int` `(` `pow` `(m, ` `0.5` `)) ` `-` ` ` `int` `(` `pow` `(n ` `-` `1` `, ` `0.5` `))) `
` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: `
` ` ` ` `n ` `=` `5` `; m ` `=` `100` `; `
` ` `print` `(` `"Count is"` `, (m ` `-` `n ` `+` `1` `) ` `-` ` ` `countOddSquares(n, m)) `
` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

`// C# implementation of the above approach ` `using` `System; `
` ` `class` `GFG `
`{ ` ` ` `// Function to count the perfect squares ` `static` `int` `countOddSquares(` `int` `n, ` `int` `m) `
`{ ` ` ` `return` `(` `int` `)Math.Pow(m, 0.5) - `
` ` `(` `int` `)Math.Pow(n - 1, 0.5); `
`} ` ` ` `// Driver code ` `static` `public` `void` `Main () `
`{ ` ` ` `int` `n = 5, m = 100; `
` ` `Console.WriteLine(` `"Count is "` `+ ((m - n + 1) `
` ` `- countOddSquares(n, m))); `
`} ` `} ` ` ` `// This Code is contributed by akt_mit. ` |

*chevron_right*

*filter_none*

`<?php ` `// PHP implementation of the ` `// above approach ` ` ` `// Function to count the perfect squares ` `function` `countOddSquares(` `$n` `, ` `$m` `) `
`{ ` ` ` `return` `(int)pow(` `$m` `, 0.5) - `
` ` `(int)pow(` `$n` `- 1, 0.5); `
`} ` ` ` `// Driver code ` `$n` `= 5; `
`$m` `= 100; `
`echo` `"Count is "` `, (` `$m` `- ` `$n` `+ 1) - `
` ` `countOddSquares(` `$n` `, ` `$m` `); `
` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Count is 88

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 odd factors in given range
- Check if a number exists having exactly N factors and K prime factors
- Sum of all even 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
- Find sum of even factors of a number
- 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
- Average of even numbers till a given even number
- Rearrange array such that all even-indexed elements in the Array is even
- Number of factors of very large number N modulo M where M is any prime number
- 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
- Sum of all odd factors of numbers in the range [l, r]
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Print even positioned nodes of even levels in level order of the given binary tree

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.

**Practice Tags :**