Given a range [n, m], find the number of elements that have odd number of factors in the given range (n and m inclusive).
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.
Count is 8
Time Complexity: O(1)
For Math.pow() refer :
Please refer complete article on Number of elements with odd factors in given range for more details!
- Program for Find sum of odd factors of a number
- Java Program for efficiently print all prime factors of a given number
- Java Program to Find sum of even factors of a number
- Java Program to Find minimum sum of factors of number
- Java Program to find Product of unique prime factors of a number
- Java Program to Find the Number Occurring Odd Number of Times
- Java Program to Store Even & Odd Elements of an Array into Separate Arrays
- Java Program to Print the Elements of an Array Present on Odd Position
- Java Program to Check if a Given Integer is Odd or Even
- Java Program to Find Maximum Odd Number in Array Using Stream and Filter
- Java Program for Odd-Even Sort / Brick Sort
- Java Program to Check if count of divisors is even or odd
- Java Program for Difference between sums of odd and even digits
- Java Program to Find the Frequency of Odd & Even Numbers in the Matrix
- Java Program to Find the Sum of First N Odd & Even Numbers
- Java Program for Range sum queries without updates
- Java Program for How to check if a given number is Fibonacci number?
- Print even and odd numbers in increasing order using two threads in Java
- Java Program for Sum the digits of a given number
- Java Program for Number of jump required of given length to reach a point of form (d, 0) from origin in 2D plane