
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 2p * 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 ( 191 ) 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
#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
- Related Articles
- Count common prime factors of two numbers in C++
- Count numbers from range whose prime factors are only 2 and 3 in C++
- Count of numbers having only 1 set bit in the range [0, n] in C++
- Count pairs of natural numbers with GCD equal to given number in C++
- Print all prime factors and their powers in C++
- Program to find count of numbers having odd number of divisors in given range in C++
- Find count of Almost Prime numbers from 1 to N in C++
- Prime numbers in a range - JavaScript
- Count number of subsets of a set with GCD equal to a given number in C++
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X in C++
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Sum of prime numbers between a range - JavaScript
- Prime numbers within a range in JavaScript
- Count of Numbers in Range where first digit is equal to last digit of the number in C++
- C++ Program to count ordinary numbers in range 1 to n
