Queries on the sum of prime factor counts in a range
Last Updated :
05 May, 2021
There are Q queries. Each query is of the form of L and R. The task is to output sum of number of prime factors of each number in the given range of each query.
Examples:
Input : Q = 2
L = 6, R = 10
L = 1, R = 5
Output : 7
4
For query 1,
6 => 2 [Prime factors are 2 and 3]
7 => 1
8 => 1
9 => 1
10 => 2
Sum = 2 + 1 + 1 + 1 + 2 = 7
For query 2,
1 => 0
2 => 1
3 => 1
4 => 1
5 => 1
Sum = 0 + 1 + 1 + 1 + 1 = 4.
Method 1 (brute force):
The idea is to traverse from L to R for each query, and for each number find the number of prime factor and add to the answer.
Method 2 (efficient approach):
The idea is to use the Sieve of Eratosthenes method for counting the number of prime factor of composite numbers. Just like, the inner loop of Sieve of Eratosthenes is used to mark composite number. We can use it for incrementing the prime factor of numbers. Instead of marking each array cell as 0 or 1, we can store the number of prime number of that index. And then for each query, find the sum of array from L to R.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
#define MAX 1000006
using namespace std;
void sieve( int count[])
{
for ( int i = 2; i * i <= MAX; i++) {
if (count[i] == 0) {
for ( int j = 2 * i; j < MAX; j += i)
count[j]++;
count[i] = 1;
}
}
}
int query( int count[], int l, int r)
{
int sum = 0;
for ( int i = l; i <= r; i++)
sum += count[i];
return sum;
}
int main()
{
int count[MAX];
memset (count, 0, sizeof count);
sieve(count);
cout << query(count, 6, 10) << endl
<< query(count, 1, 5);
return 0;
}
|
Java
class GFG {
static final int MAX = 1000006 ;
static void sieve( int count[])
{
for ( int i = 2 ; i * i <= MAX; i++) {
if (count[i] == 0 ) {
for ( int j = 2 * i; j < MAX; j += i)
count[j]++;
count[i] = 1 ;
}
}
}
static int query( int count[], int l, int r)
{
int sum = 0 ;
for ( int i = l; i <= r; i++)
sum += count[i];
return sum;
}
public static void main(String[] args)
{
int count[] = new int [MAX];
sieve(count);
System.out.println(query(count, 6 , 10 ) + " " + query(count, 1 , 5 ));
}
}
|
Python3
MAX = 100006 ;
count = [ 0 ] * MAX ;
def sieve():
i = 2 ;
while (i * i < = MAX ):
if (count[i] = = 0 ):
for j in range ( 2 * i, MAX , i):
count[j] + = 1 ;
count[i] = 1 ;
i + = 1 ;
def query(l, r):
sum = 0 ;
for i in range (l, r + 1 ):
sum + = count[i];
return sum ;
sieve();
print (query( 6 , 10 ), query( 1 , 5 ));
|
C#
using System;
class GFG {
static int MAX = 1000006;
static void sieve( int [] count)
{
for ( int i = 2; i * i <= MAX; i++)
{
if (count[i] == 0) {
for ( int j = 2 * i; j < MAX;
j += i)
count[j]++;
count[i] = 1;
}
}
}
static int query( int [] count, int l, int r)
{
int sum = 0;
for ( int i = l; i <= r; i++)
sum += count[i];
return sum;
}
public static void Main()
{
int [] count = new int [MAX];
sieve(count);
Console.Write(query(count, 6, 10) +
" " + query(count, 1, 5));
}
}
|
PHP
<?php
$MAX = 100006;
function sieve(& $count )
{
global $MAX ;
for ( $i = 2; $i * $i <= $MAX ; $i ++)
{
if ( $count [ $i ] == 0)
{
for ( $j = 2 * $i ; $j < $MAX ; $j += $i )
$count [ $j ]++;
$count [ $i ] = 1;
}
}
}
function query( $count , $l , $r )
{
$sum = 0;
for ( $i = $l ; $i <= $r ; $i ++)
$sum += $count [ $i ];
return $sum ;
}
$count = array_fill (0, $MAX , 0);
sieve( $count );
echo query( $count , 6, 10) . " " .
query( $count , 1, 5);
?>
|
Javascript
<script>
var MAX = 1000006;
function sieve(count)
{
for (i = 2; i * i <= MAX; i++) {
if (count[i] == 0) {
for (j = 2 * i; j < MAX; j += i)
count[j]++;
count[i] = 1;
}
}
}
function query(count , l , r)
{
var sum = 0;
for (i = l; i <= r; i++)
sum += count[i];
return sum;
}
var count = Array.from({length: MAX},
(_, i) => 0);
sieve(count);
document.write(query(count, 6, 10) + " " +
query(count, 1, 5));
</script>
|
Output:
7 4
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...