Given a range represented by two positive integers L and R. The task is to count the numbers from the range having GCD of powers of prime factors equal to 1. In other words, if a number X has its prime factorization of the form 2p1 * 3p2 * 5p3 * … then the GCD of p1, p2, p3, … should be equal to 1.
Input: L = 2, R = 5
2, 3, and 5 are the required numbers having GCD of powers of prime factors equal to 1.
2 = 21
3 = 31
5 = 51
Input: L = 13, R = 20
Prerequisites: Perfect Powers in a Range
Naive Approach: Iterate over all numbers from L to R and prime factorise each number then calculate the GCD of powers of the prime factors. If the GCD = 1, increment a count variable and finally return it as the answer.
Efficient Approach: The key idea here is to notice that the valid numbers are not perfect powers since the powers of prime factors number are in such a way that their GCD is always greater than 1. In other words, all perfect powers are not valid numbers.
2500 is perfect power whose prime factorization is 2500 = 22 * 54. Now the GCD of (2, 4) = 2 which is greater than 1.
If some number has xth power of a factor in its prime factorization, then the powers of other prime factors will have to be multiples of x in order for the number to be invalid.
Hence, we can find the total number of perfect powers lying in the range and subtract it from the total numbers.
Below is the implementation of the above approach:
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count numbers in a given range whose count of prime factors is a Prime Number
- Check if a number exists having exactly N factors and K prime factors
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Print all prime factors and their powers
- Smallest subsequence having GCD equal to GCD of given array
- Count of quadruplets from range [L, R] having GCD equal to K
- Count numbers from range whose prime factors are only 2 and 3
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Maximum number of prime factors a number can have with exactly x factors
- Sum of largest divisible powers of p (a prime number) in a range
- K-Primes (Numbers with k prime factors) in a range
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Find and Count total factors of co-prime A or B in a given range 1 to N
- Count of all possible pairs having sum of LCM and GCD equal to N
- Count all prime numbers in a given range whose sum of digits is also prime
- First element of every K sets having consecutive elements with exactly K prime factors less than N
- Count common prime factors of two numbers
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Find number of factors of N when location of its two factors whose product is N is given
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.