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 **2 ^{p1} * 3^{p2} * 5^{p3} * …** then the GCD of

**p**should be equal to

_{1}, p_{2}, p_{3}, …**1**.

**Examples:**

Input:L = 2, R = 5Output:3

2, 3, and 5 are the required numbers having GCD of powers of prime factors equal to 1.

2 = 2^{1}

3 = 3^{1}

5 = 5^{1}

Input:L = 13, R = 20Output:7

**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.**For e.g.**

2500 is perfect power whose prime factorization is 2500 = 2

^{2}* 5^{4}. Now the GCD of (2, 4) = 2 which is greater than 1.

If some number has x^{th}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:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; `
` ` `#define N 1000005 ` `#define MAX 1e18 ` ` ` `// Vector to store powers greater than 3 ` `vector<` `long` `int` `> powers; `
` ` `// Set to store perfect squares ` `set<` `long` `int` `> squares; `
` ` `// Set to store powers other than perfect squares ` `set<` `long` `int` `> s; `
` ` `void` `powersPrecomputation() `
`{ ` ` ` `for` `(` `long` `int` `i = 2; i < N; i++) { `
` ` ` ` `// Pushing squares `
` ` `squares.insert(i * i); `
` ` ` ` `// if the values is already a perfect square means `
` ` `// present in the set `
` ` `if` `(squares.find(i) != squares.end()) `
` ` `continue` `; `
` ` ` ` `long` `int` `temp = i; `
` ` ` ` `// Run loop until some power of current number `
` ` `// doesn't exceed MAX `
` ` `while` `(i * i <= MAX / temp) { `
` ` `temp *= (i * i); `
` ` ` ` `// Pushing only odd powers as even power of a number `
` ` `// can always be expressed as a perfect square `
` ` `// which is already present in set squares `
` ` `s.insert(temp); `
` ` `} `
` ` `} `
` ` ` ` `// Inserting those sorted `
` ` `// values of set into a vector `
` ` `for` `(` `auto` `x : s) `
` ` `powers.push_back(x); `
`} ` ` ` `long` `int` `calculateAnswer(` `long` `int` `L, ` `long` `int` `R) `
`{ ` ` ` ` ` `// Precompute the powers `
` ` `powersPrecomputation(); `
` ` ` ` `// Calculate perfect squares in `
` ` `// range using sqrtl function `
` ` `long` `int` `perfectSquares = ` `floor` `(sqrtl(R)) - ` `floor` `(sqrtl(L - 1)); `
` ` ` ` `// Calculate upper value of R `
` ` `// in vector using binary search `
` ` `long` `int` `high = upper_bound(powers.begin(), `
` ` `powers.end(), R) `
` ` `- powers.begin(); `
` ` ` ` `// Calculate lower value of L `
` ` `// in vector using binary search `
` ` `long` `int` `low = lower_bound(powers.begin(), `
` ` `powers.end(), L) `
` ` `- powers.begin(); `
` ` ` ` `// Calculate perfect powers `
` ` `long` `perfectPowers = perfectSquares + (high - low); `
` ` ` ` `// Compute final answer `
` ` `long` `ans = (R - L + 1) - perfectPowers; `
` ` `return` `ans; `
`} ` ` ` `// Driver Code ` `int` `main() `
`{ ` ` ` `long` `int` `L = 13, R = 20; `
` ` `cout << calculateAnswer(L, R); `
` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`# Python3 implementation of the approach ` `from` `bisect ` `import` `bisect as upper_bound `
`from` `bisect ` `import` `bisect_left as lower_bound `
`from` `math ` `import` `floor `
`N ` `=` `1000005`
`MAX` `=` `10` `*` `*` `18`
` ` `# Vector to store powers greater than 3 ` `powers ` `=` `[] `
` ` `# Set to store perfect squares ` `squares ` `=` `dict` `() `
` ` `# Set to store powers other than perfect squares ` `s ` `=` `dict` `() `
` ` `def` `powersPrecomputation(): `
` ` ` ` `for` `i ` `in` `range` `(` `2` `, N): `
` ` ` ` `# Pushing squares `
` ` `squares[i ` `*` `i] ` `=` `1`
` ` ` ` `# if the values is already a perfect square means `
` ` `# present in the set `
` ` `if` `(i ` `not` `in` `squares.keys()): `
` ` `continue`
` ` ` ` `temp ` `=` `i `
` ` ` ` `# Run loop until some power of current number `
` ` `# doesn't exceed MAX `
` ` `while` `(i ` `*` `i <` `=` `(` `MAX` `/` `/` `temp)): `
` ` `temp ` `*` `=` `(i ` `*` `i) `
` ` ` ` `# Pushing only odd powers as even power of a number `
` ` `# can always be expressed as a perfect square `
` ` `# which is already present in set squares `
` ` `s[temp]` `=` `1`
` ` ` ` `# Inserting those sorted `
` ` `# values of set into a vector `
` ` `for` `x ` `in` `s: `
` ` `powers.append(x) `
` ` `def` `calculateAnswer(L, R): `
` ` ` ` `# Precompute the powers `
` ` `powersPrecomputation() `
` ` ` ` `# Calculate perfect squares in `
` ` `# range using sqrtl function `
` ` `perfectSquares ` `=` `floor((R)` `*` `*` `(.` `5` `)) ` `-` `floor((L ` `-` `1` `)` `*` `*` `(.` `5` `)) `
` ` ` ` `# Calculate upper value of R `
` ` `# in vector using binary search `
` ` `high ` `=` `upper_bound(powers,R) `
` ` ` ` `# Calculate lower value of L `
` ` `# in vector using binary search `
` ` `low ` `=` `lower_bound(powers,L) `
` ` ` ` `# Calculate perfect powers `
` ` `perfectPowers ` `=` `perfectSquares ` `+` `(high ` `-` `low) `
` ` ` ` `# Compute final answer `
` ` `ans ` `=` `(R ` `-` `L ` `+` `1` `) ` `-` `perfectPowers `
` ` ` ` `return` `ans `
` ` ` ` `# Driver Code ` ` ` `L ` `=` `13`
`R ` `=` `20`
`print` `(calculateAnswer(L, R)) `
` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

7

## Recommended Posts:

- 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
- Queries for the difference between the count of composite and prime numbers in a given range
- Sum of largest divisible powers of p (a prime number) in a range
- Common prime factors of two numbers
- Sum of all odd factors of numbers in the range [l, r]
- Sum of all even 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

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

**Article Tags :**