# Count numbers in a range having GCD of powers of prime factors equal to 1 in C++

Given two numbers start and end representing a range of positive integers. The goal is to find the count of all the numbers that lie in range [start,end] and have prime factorization such that all prime factors of that number have powers such that they have GCD as 1.

If a number has prime factorization as 2* 3q * 5r ….. Then powers p,q,r ...should have gcd=1.

## Let us understand with examples.

For Example

Input - start = 1, end = 10

Output - Count of numbers in a range having GCD of powers of prime factors equal to 1 are: 6

Explanation - The numbers are:

2 ( 21 ), 3 ( 31 ), 5 ( 51 ), 7 ( 71 ), 8 ( 23 ) and 10 ( 21*51 ). All powers of each prime factorization have gcd as 1.

Input - start = 11, end = 20

Output - Count of numbers in a range having GCD of powers of prime factors equal to 1 are: 9

Explanation - The numbers are:

11 ( 111 ), 12 ( 31*22 ), 13 ( 131 ), 14 ( 21*71 ), 15 ( 31*51 ), 17 ( 171 ), 18 ( 21*32 ), 19 ( 19) and 20 ( 22*51 ). All powers of each prime factorization have gcd as 1.

## Approach used in the below program is as follows

In this approach we will count all the numbers in the range start to end that are not perfect powers. As non-perfect powers will satisfy the above condition. For this we will find all perfect powers and remove them from total count.

Answer will be = start-end +1 - ( count of numbers in range[start,end] that are perfect powers ).

• Take range variables start and end as input.
• Take vector vec to store powers greater than 3.
• Take a set sett to store numbers that are perfect squares.
• Take a set sett_2 to store numbers that are not perfect squares.
• Function calculate() populates vector vec and sets sett and sett_2. To separate numbers that are perfect squares, non perfect squares and powers>3.
• Travers using for loop from i=2 to i<size.
• Insert perfect powers i*i to sett.
• IF sett.find(i) != sett.end()) returns true then i is a perfect square and present in sett so do nothing.
• Run while loop until power of current number remains less than large.
• Insert odd powers to sett_2 as even powers are perfect squares and in sett.
• At the end insert sorted values of sett_2 to vector vec using for loop.
• Function GCD_1(long int start, long int end) takes range as input and returns the count of numbers in a range having GCD of powers of prime factors equal to 1.
• Call calculate().
• Calculate perfect squares in range as per_sq = floor(sqrtl(end)) - floor(sqrtl(start - 1)).
• Calculate upper value of start in vec using upper_bound(vec.begin(), vec.end(), end) - vec.begin().
• Similarly lower value of end in vec using bottom = lower_bound(vec.begin(), vec.end(), start) - vec.begin().
• Calculate perfect powers as per_pow = per_sq + (top - bottom).
• Answer will be count = (end - start + 1) - per_pow.
• Return count as result at the end.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
#define size 1000005
#define large 1e18

vector < long int > vec;
set < long int > sett;
set < long int > sett_2;

void calculate() {
for (long int i = 2; i < size; i++) {
sett.insert(i * i);
if (sett.find(i) != sett.end()) {
continue;
}
long int total = i;
while (i * i <= large / total) {
total *= (i * i);
sett_2.insert(total);
}
}
for (auto it: sett_2) {
vec.push_back(it);
}
}

long int GCD_1(long int start, long int end) {
calculate();

long int per_sq = floor(sqrtl(end)) - floor(sqrtl(start - 1));
long int top = upper_bound(vec.begin(), vec.end(), end) - vec.begin();
long int bottom = lower_bound(vec.begin(), vec.end(), start) - vec.begin();
long int per_pow = per_sq + (top - bottom);
long int count = (end - start + 1) - per_pow;
return count;
}
int main() {
long int start = 10, end = 40;
cout << "Count of numbers in a range having GCD of powers of prime factors equal to 1 are: " << GCD_1(start, end);
return 0;
}

If we run the above code it will generate the following output −

## Output

Count of numbers in a range having GCD of powers of prime factors equal to 1 are: 7