Given two numbers a and b, and a number k which is odd. The task is to find all the numbers between a and b (both inclusive) having exactly k divisors.
Input : a = 2, b = 49, k = 3 Output: 4 // Between 2 and 49 there are four numbers // with three divisors // 4 (Divisors 1, 2, 4), 9 (Divisors 1, 3, 9), // 25 (Divisors 1, 5, 25} and 49 (1, 7 and 49) Input : a = 1, b = 100, k = 9 Output: 2 // between 1 and 100 there are 36 (1, 2, 3, 4, 6, 9, 12, 18, 36) // and 100 (1, 2, 4, 5, 10, 20, 25, 50, 100) having exactly 9 // divisors
This problem has simple solution, here we are given that k is odd and we know that only perfect square numbers have odd number of divisors , so we just need to check all perfect square numbers between a and b, and calculate divisors of only those perfect square numbers.
This problem can be solved more efficiently. Please refer method 2 of below post for an efficient solution.
This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
- Find numbers with n-divisors in a given range
- Find the number of divisors of all numbers in the range [1, n]
- Program to find count of numbers having odd number of divisors in given range
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Find two numbers whose divisors are given in a random order
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Find sum of divisors of all the divisors of a natural number
- Find XOR of numbers from the range [L, R]
- Find a range of composite numbers of given length
- Find kth smallest number in range [1, n] when all the odd numbers are deleted
- Count total divisors of A or B in a given range
- Find the highest occurring digit in prime numbers in a range
- Count elements in the given range which have maximum number of divisors
Improved By : nitin mittal