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:
- Count numbers from range whose prime factors are only 2 and 3
- K-Primes (Numbers with k prime factors) in a range
- Count common prime factors of two numbers
- Print all prime factors and their powers
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Sum of largest divisible powers of p (a prime number) in a range
- Common prime factors of two numbers
- Sum of all even factors of numbers in the range [l, r]
- Sum of all odd factors of numbers in the range [l, r]
- Count of Numbers in Range where first digit is equal to last digit of the number
- Sort an array according to the increasing count of distinct Prime Factors
- Number of distinct prime factors of first n natural numbers
- Prime numbers in a given range using STL | Set 2
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.