Count numbers in a range having GCD of powers of prime factors equal to 1
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.
Examples:
Input: L = 2, R = 5
Output: 3 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
Output: 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 = 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:
CPP
#include <bits/stdc++.h>
using namespace std;
#define N 1000005
#define MAX 1e18
vector< long int > powers;
set< long int > squares;
set< long int > s;
void powersPrecomputation()
{
for ( long int i = 2; i < N; i++) {
squares.insert(i * i);
if (squares.find(i) != squares.end())
continue ;
long int temp = i;
while (i * i <= MAX / temp) {
temp *= (i * i);
s.insert(temp);
}
}
for ( auto x : s)
powers.push_back(x);
}
long int calculateAnswer( long int L, long int R)
{
powersPrecomputation();
long int perfectSquares = floor (sqrtl(R)) - floor (sqrtl(L - 1));
long int high = upper_bound(powers.begin(),
powers.end(), R)
- powers.begin();
long int low = lower_bound(powers.begin(),
powers.end(), L)
- powers.begin();
long perfectPowers = perfectSquares + (high - low);
long ans = (R - L + 1) - perfectPowers;
return ans;
}
int main()
{
long int L = 13, R = 20;
cout << calculateAnswer(L, R);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int N = 1000005 ;
static long MAX = ( long ) 1e18;
static Vector<Long> powers = new Vector<>();
static TreeSet<Long> squares = new TreeSet<>();
static TreeSet<Long> s = new TreeSet<>();
static void powersPrecomputation()
{
for ( long i = 2 ; i < N; i++)
{
squares.add(i * i);
if (squares.contains(i))
continue ;
long temp = i;
while (i * i <= MAX / temp)
{
temp *= (i * i);
s.add(temp);
}
}
for ( long x : s)
powers.add(x);
}
static long calculateAnswer( long L, long R)
{
powersPrecomputation();
long perfectSquares = ( long ) (Math.floor(Math.sqrt(R)) -
Math.floor(Math.sqrt(L - 1 )));
long high = Collections.binarySearch(powers, R);
long low = Collections.binarySearch(powers, L);
long perfectPowers = perfectSquares + (high - low);
long ans = (R - L + 1 ) - perfectPowers;
return ans;
}
public static void main(String[] args)
{
long L = 13 , R = 20 ;
System.out.println(calculateAnswer(L, R));
}
}
|
Python3
from bisect import bisect as upper_bound
from bisect import bisect_left as lower_bound
from math import floor
N = 1000005
MAX = 10 * * 18
powers = []
squares = dict ()
s = dict ()
def powersPrecomputation():
for i in range ( 2 , N):
squares[i * i] = 1
if (i not in squares.keys()):
continue
temp = i
while (i * i < = ( MAX / / temp)):
temp * = (i * i)
s[temp] = 1
for x in s:
powers.append(x)
def calculateAnswer(L, R):
powersPrecomputation()
perfectSquares = floor((R) * * (. 5 )) - floor((L - 1 ) * * (. 5 ))
high = upper_bound(powers,R)
low = lower_bound(powers,L)
perfectPowers = perfectSquares + (high - low)
ans = (R - L + 1 ) - perfectPowers
return ans
L = 13
R = 20
print (calculateAnswer(L, R))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int N = 100005;
static long MAX = ( long ) 1e18;
static List< long > powers = new List< long >();
static HashSet< long > squares = new HashSet< long >();
static HashSet< long > s = new HashSet< long >();
static void powersPrecomputation()
{
for ( long i = 2; i < N; i++)
{
squares.Add(i * i);
if (squares.Contains(i))
continue ;
long temp = i;
while (i * i <= MAX / temp)
{
temp *= (i * i);
s.Add(temp);
}
}
foreach ( long x in s)
powers.Add(x);
}
static long calculateAnswer( long L, long R)
{
powersPrecomputation();
long perfectSquares = ( long ) (Math.Floor(Math.Sqrt(R)) -
Math.Floor(Math.Sqrt(L - 1)));
long high = Array.BinarySearch(powers.ToArray(), R);
long low = Array.BinarySearch(powers.ToArray(), L);
long perfectPowers = perfectSquares + (high - low);
long ans = (R - L + 1) - perfectPowers;
return ans;
}
public static void Main(String[] args)
{
long L = 13, R = 20;
Console.WriteLine(calculateAnswer(L, R));
}
}
|
Javascript
let N = 1000005;
let MAX = 1e18;
let powers = [];
let squares = new Set();
let s = new Set();
function upper_bound(arr, value)
{
for (let i = 0; i < arr.length; i++)
{
if (arr[i] > value)
return i;
}
return arr.length;
}
function lower_bound(arr, value)
{
for (let i = 0; i < arr.length; i++)
{
if (arr[i] >= value)
return i;
}
return arr.length;
}
function powersPrecomputation()
{
for (let i = 2; i < N; i++) {
squares.add(i * i);
if (squares.has(i))
continue ;
let temp = i;
while (i * i <= MAX / temp) {
temp *= (i * i);
s.add(temp);
}
}
for (let x of s)
powers.push(x);
}
function calculateAnswer(L, R)
{
powersPrecomputation();
let perfectSquares = Math.floor(Math.sqrt(R)) - Math.floor(Math.sqrt(L - 1));
let high = upper_bound(powers, R);
let low = lower_bound(powers, L);
let perfectPowers = perfectSquares + (high - low);
let ans = (R - L + 1) - perfectPowers;
return ans;
}
let L = 13, R = 20;
console.log(calculateAnswer(L, R));
|
Time Complexity: O(N * log N), to iterate over N
Auxiliary Space: O(N), since N extra space has been taken.
Last Updated :
06 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...