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).
Input: n = 5, m = 20 Output: 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 = 100 Output: 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:
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.
- Find number of factors of N when location of its two factors whose product is N is given
- Check if a number has an odd count of odd divisors and even count of even divisors
- Generate K co-prime pairs of factors of a given number
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Count elements in the given range which have maximum number of divisors
- Count the factors of K present in the given Array
- Sum of Factors of a Number using Prime Factorization
- Maximize the product of four factors of a Number
- Program to find all Factors of a Number using recursion
- Check if there exists a number with X factors out of which exactly K are prime
- Perfect Square factors of a Number
- Perfect Cube factors of a Number
- Count of elements having odd number of divisors in index range [L, R] for Q queries
- Array range queries to find the number of perfect square elements with updates
- Find four factors of N with maximum product and sum equal to N | Set-2
- Find four factors of N with maximum product and sum equal to N
- Find four factors of N with maximum product and sum equal to N | Set 3
- Find numbers which are multiples of first array and factors of second array
- Sum of M maximum distinct digit sum from 1 to N that are factors of K
- Count of nodes in a Binary tree with immediate children as its factors
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.