- Related Questions & Answers
- Count common prime factors of two numbers in C++
- Count of numbers having only 1 set bit in the range [0, n] in C++
- Print all prime factors and their powers in C++
- Count pairs of natural numbers with GCD equal to given number in C++
- Count numbers from range whose prime factors are only 2 and 3 in C++
- Find count of Almost Prime numbers from 1 to N in C++
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X in C++
- Program to find count of numbers having odd number of divisors in given range in C++
- Count number of sub-sequences with GCD 1 in C++
- Prime factors of a big number in C++
- Count number of subsets of a set with GCD equal to a given number in C++
- Sum of prime numbers between a range - JavaScript
- Prime numbers in a range - JavaScript
- Prime factors of LCM of array elements in C++
- Count of Numbers in Range where first digit is equal to last digit of the number in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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^{p }* 3^{q} * 5^{r} ….. Then powers p,q,r ...should have gcd=1.

**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 ( 2^{1} ), 3 ( 3^{1} ), 5 ( 5^{1} ), 7 ( 7^{1} ), 8 ( 2^{3} ) and 10 ( 2^{1}*5^{1} ). 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 ( 11^{1} ), 12 ( 3^{1}*2^{2} ), 13 ( 13^{1} ), 14 ( 2^{1}*7^{1} ), 15 ( 3^{1}*5^{1} ), 17 ( 17^{1} ), 18 ( 2^{1}*3^{2} ), 19 ( 19^{1 }) and 20 ( 2^{2}*5^{1} ). All powers of each prime factorization have gcd as 1.

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.

#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 −

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

Advertisements