Count numbers in a given range whose count of prime factors is a Prime Number
Last Updated :
17 May, 2021
Given a 2D array Q[][] of size N * 2 representing queries of the form {L, R} . For each query, the task is to print the count of numbers in the range [L, R] with a count of prime factors equal to a prime number .
Examples:
Input: Q[][] = {{4, 8}, {30, 32}} Output: 3 2 Explanation: Query 1: Prime factors of 4 = {2, 2} and count of prime factors = 2 Prime factors of 5 = {5} and count of prime factors = 1 Prime factors of 6 = {2, 3} and count of prime factors = 2 Prime factors of 7 = {7} and count of prime factors = 1 Prime factors of 8 = {2, 2, 2} and count of prime factors = 3 Therefore, the total count of numbers in the range [4, 8] having count of prime factors is a prime number is 3. Query 2: Prime factors of 30 = {2, 3, 5} and count of prime factors = 3 Prime factors of 31 = {31} and count of prime factors = 1 Prime factors of 32 = {2, 2, 2, 2, 2} and count of prime factors = 5 Therefore, the total count of numbers in the range [4, 8] having count of prime factors is a prime number is 2.
Input: Q[][] = {{7, 12}, {10, 99}} Output: 4
Naive Approach: The simplest approach to solve this problem is to traverse all the numbers in the range [L, R] , and for each number, check if the count of prime factors of the number is a prime number or not. If found to be true, increment the counter by 1 . After traversing, print the value of counter for each query.
Time Complexity: O(|Q| * (max(arr[i][1] – arr[i][0] + 1)) * sqrt(max(arr[i][1])) Auxiliary space: O (1)
Efficient Approach: To optimize the above approach the idea is to precompute the smallest prime factor of each number in the range [Li , Ri ] using Sieve of Eratosthenes . Follow the steps below to solve the problem:
Generate and store the smallest prime factor of each element using Sieve of Eratosthenes .
Find the count of prime factors for each number in the range [Li , Ri ] using the Sieve .
For each number, check if the total count of prime factors is a prime number or not. If found to be true then increment the counter.
Create a prefix sum array, say sum[], where sum[i] will store the sum of elements from the range [0, i] whose count of prime factors is a prime number.
Finally, for each query, print the value sum[arr[i][1]] – sum[arr[i][0] – 1] .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1001
vector<int> sieve()
{
vector<int> spf(MAX);
spf[0] = spf[1] = -1;
for (int i = 2; i < MAX; i++) {
spf[i] = i;
}
for (int i = 4; i < MAX; i = i + 2) {
spf[i] = 2;
}
for (int i = 3; i * i < MAX; i++) {
if (spf[i] == i) {
for (int j = i * i; j < MAX;
j = j + i) {
if (spf[j] == j) {
spf[j] = i;
}
}
}
}
return spf;
}
int countFactors(vector<int>& spf, int num)
{
int count = 0;
while (num > 1) {
count++;
num = num / spf[num];
}
return count;
}
vector<int> precalculateSum(vector<int>& spf)
{
vector<int> sum(MAX);
sum[0] = 0;
for (int i = 1; i < MAX; i++) {
int prime_factor
= countFactors(spf, i);
if (spf[prime_factor] == prime_factor) {
sum[i] = sum[i - 1] + 1;
}
else {
sum[i] = sum[i - 1];
}
}
return sum;
}
int main()
{
vector<int> spf = sieve();
vector<int> sum = precalculateSum(spf);
int Q[][2] = { { 4, 8 }, { 30, 32 } };
for (int i = 0; i < 2; i++) {
cout << (sum[Q[i][1]] - sum[Q[i][0] - 1])
<< " ";
}
return 0;
}
Java
import java.util.*;
class GFG{
public static int MAX = 1001;
public static int[] sieve()
{
int spf[] = new int[MAX];
spf[0] = spf[1] = -1;
for(int i = 2; i < MAX; i++)
{
spf[i] = i;
}
for(int i = 4; i < MAX; i = i + 2)
{
spf[i] = 2;
}
for(int i = 3; i * i < MAX; i++)
{
if (spf[i] == i)
{
for(int j = i * i; j < MAX; j = j + i)
{
if (spf[j] == j)
{
spf[j] = i;
}
}
}
}
return spf;
}
public static int countFactors(int spf[], int num)
{
int count = 0;
while (num > 1)
{
count++;
num = num / spf[num];
}
return count;
}
public static int[] precalculateSum(int spf[])
{
int sum[] = new int[MAX];
sum[0] = 0;
for(int i = 1; i < MAX; i++)
{
int prime_factor = countFactors(spf, i);
if (spf[prime_factor] == prime_factor)
{
sum[i] = sum[i - 1] + 1;
}
else
{
sum[i] = sum[i - 1];
}
}
return sum;
}
public static void main(String[] args)
{
int spf[] = sieve();
int sum[] = precalculateSum(spf);
int Q[][] = { { 4, 8 }, { 30, 32 } };
for(int i = 0; i < 2; i++)
{
System.out.print((sum[Q[i][1]] -
sum[Q[i][0] - 1]) + " ");
}
}
}
Python3
MAX = 1001
def sieve():
global MAX
spf = [0] * MAX
spf[0] = spf[1] = -1
for i in range(2, MAX):
spf[i] = i
for i in range(4, MAX, 2):
spf[i] = 2
for i in range(3, MAX):
if (spf[i] == i):
for j in range(i * i, MAX):
if (spf[j] == j):
spf[j] = i
return spf
def countFactors(spf, num):
count = 0
while (num > 1):
count += 1
num = num // spf[num]
return count
def precalculateSum(spf):
sum = [0] * MAX
for i in range(1, MAX):
prime_factor = countFactors(spf, i)
if (spf[prime_factor] == prime_factor):
sum[i] = sum[i - 1] + 1
else:
sum[i] = sum[i - 1]
return sum
if __name__ == '__main__':
spf = sieve()
sum = precalculateSum(spf)
Q = [ [ 4, 8 ], [ 30, 32 ] ]
sum[Q[0][1]] += 1
for i in range(0, 2):
print((sum[Q[i][1]] -
sum[Q[i][0]]), end = " ")
C#
using System;
class GFG{
public static int MAX = 1001;
public static int[] sieve()
{
int []spf = new int[MAX];
spf[0] = spf[1] = -1;
for(int i = 2; i < MAX; i++)
{
spf[i] = i;
}
for(int i = 4; i < MAX; i = i + 2)
{
spf[i] = 2;
}
for(int i = 3; i * i < MAX; i++)
{
if (spf[i] == i)
{
for(int j = i * i;
j < MAX; j = j + i)
{
if (spf[j] == j)
{
spf[j] = i;
}
}
}
}
return spf;
}
public static int countFactors(int []spf,
int num)
{
int count = 0;
while (num > 1)
{
count++;
num = num / spf[num];
}
return count;
}
public static int[] precalculateSum(int []spf)
{
int []sum = new int[MAX];
sum[0] = 0;
for(int i = 1; i < MAX; i++)
{
int prime_factor = countFactors(spf, i);
if (spf[prime_factor] == prime_factor)
{
sum[i] = sum[i - 1] + 1;
}
else
{
sum[i] = sum[i - 1];
}
}
return sum;
}
public static void Main(String[] args)
{
int []spf = sieve();
int []sum = precalculateSum(spf);
int [,]Q = {{4, 8}, {30, 32}};
for(int i = 0; i < 2; i++)
{
Console.Write((sum[Q[i, 1]] -
sum[Q[i, 0] - 1]) +
" ");
}
}
}
Javascript
<script>
let MAX = 1001
function sieve()
{
let spf = new Array(MAX);
spf[0] = spf[1] = -1;
for (let i = 2; i < MAX; i++) {
spf[i] = i;
}
for (let i = 4; i < MAX; i = i + 2) {
spf[i] = 2;
}
for (let i = 3; i * i < MAX; i++) {
if (spf[i] == i) {
for (let j = i * i; j < MAX;
j = j + i) {
if (spf[j] == j) {
spf[j] = i;
}
}
}
}
return spf;
}
function countFactors(spf, num)
{
let count = 0;
while (num > 1) {
count++;
num = num / spf[num];
}
return count;
}
function precalculateSum(spf)
{
let sum = new Array(MAX);
sum[0] = 0;
for (let i = 1; i < MAX; i++) {
let prime_factor
= countFactors(spf, i);
if (spf[prime_factor] == prime_factor) {
sum[i] = sum[i - 1] + 1;
}
else {
sum[i] = sum[i - 1];
}
}
return sum;
}
let spf = sieve();
let sum = precalculateSum(spf);
let Q = [ [ 4, 8 ], [ 30, 32 ] ];
for (let i = 0; i < 2; i++) {
document.write((sum[Q[i][1]] - sum[Q[i][0] - 1]) +
" ");
}
</script>
Time Complexity: O(|Q| + (MAX *log(log(MAX))))Auxiliary Space: O(MAX)
Share your thoughts in the comments
Add Your Comment
Please Login to comment...