Count occurrences of a prime number in the prime factorization of every element from the given range
Given three integers L, R, and P where P is prime, the task is to count the number of times P occurs in the prime factorization of all numbers in the range [L, R].
Examples:
Input: L = 2, R = 8, P = 2
Output: 7
Element |
Prime Factors |
Time 2 occurs |
2 |
2 |
1 |
3 |
3 |
0 |
4 |
2 * 2 |
2 |
5 |
5 |
0 |
6 |
2 * 3 |
1 |
7 |
7 |
0 |
8 |
2 * 2 * 2 |
3 |
1 + 0 + 2 + 0 + 1 + 0 + 3 = 7
Input: L = 5, R = 25, P = 7
Output: 3
Naive approach: Simply iterate over the range and for each value count how many times P divides that value and sum them up for the result. Time complexity O(R – L).
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int countFactors( int n, int p)
{
int pwr = 0;
while (n > 0 && n % p == 0) {
n /= p;
pwr++;
}
return pwr;
}
int getCount( int l, int r, int p)
{
int cnt = 0;
for ( int i = l; i <= r; i++) {
cnt += countFactors(i, p);
}
return cnt;
}
int main()
{
int l = 2, r = 8, p = 2;
cout << getCount(l, r, p);
return 0;
}
|
Java
class GFG
{
static int countFactors( int n, int p)
{
int pwr = 0 ;
while (n > 0 && n % p == 0 )
{
n /= p;
pwr++;
}
return pwr;
}
static int getCount( int l, int r, int p)
{
int cnt = 0 ;
for ( int i = l; i <= r; i++)
{
cnt += countFactors(i, p);
}
return cnt;
}
public static void main(String[] args)
{
int l = 2 , r = 8 , p = 2 ;
System.out.println(getCount(l, r, p));
}
}
|
Python3
def countFactors(n, p) :
pwr = 0 ;
while (n > 0 and n % p = = 0 ) :
n / / = p;
pwr + = 1 ;
return pwr;
def getCount(l, r, p) :
cnt = 0 ;
for i in range (l, r + 1 ) :
cnt + = countFactors(i, p);
return cnt;
if __name__ = = "__main__" :
l = 2 ; r = 8 ; p = 2 ;
print (getCount(l, r, p));
|
C#
using System;
class GFG
{
static int countFactors( int n, int p)
{
int pwr = 0;
while (n > 0 && n % p == 0)
{
n /= p;
pwr++;
}
return pwr;
}
static int getCount( int l, int r, int p)
{
int cnt = 0;
for ( int i = l; i <= r; i++)
{
cnt += countFactors(i, p);
}
return cnt;
}
public static void Main(String[] args)
{
int l = 2, r = 8, p = 2;
Console.WriteLine(getCount(l, r, p));
}
}
|
Javascript
<script>
function countFactors(n, p)
{
let pwr = 0;
while (n > 0 && n % p == 0) {
n /= p;
pwr++;
}
return pwr;
}
function getCount(l, r, p)
{
let cnt = 0;
for (let i = l; i <= r; i++) {
cnt += countFactors(i, p);
}
return cnt;
}
let l = 2, r = 8, p = 2;
document.write(getCount(l, r, p));
</script>
|
Time complexity : O((r-l+1)*log p) because the for loop runs (r-l+1) times and each iteration of the loop takes log p time due to the countFactors() function.
Auxiliary Space: O(1)
Efficient approach: Count the values which are divisible by P, P2, P3, …, Px in the range [L, R] where x is the largest power of P such that Px lies within the upper bound (Px ? N). Each iteration cost O(1) time thus time complexity becomes O(x) where x = (log(R) / log(P)).
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int getCount( int l, int r, int p)
{
int cnt = 0;
int val = p;
while (1) {
int a = r / val;
int b = (l - 1) / val;
val *= p;
if (a - b) {
cnt += (a - b);
}
else
break ;
}
return cnt;
}
int main()
{
int l = 2, r = 8, p = 2;
cout << getCount(l, r, p);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int getCount( int l, int r, int p)
{
int cnt = 0 ;
int val = p;
while ( true )
{
int a = r / val;
int b = (l - 1 ) / val;
val *= p;
if ((a - b) > 0 )
{
cnt += (a - b);
}
else
break ;
}
return cnt;
}
public static void main(String[] args)
{
int l = 2 , r = 8 , p = 2 ;
System.out.println(getCount(l, r, p));
}
}
|
Python3
def getCount(l, r, p):
cnt = 0 ;
val = p;
while ( True ):
a = r / / val;
b = (l - 1 ) / / val;
val * = p;
if (a - b):
cnt + = (a - b);
else :
break ;
return int (cnt);
l = 2 ;
r = 8 ;
p = 2 ;
print (getCount(l, r, p));
|
C#
using System;
class GFG
{
static int getCount( int l, int r, int p)
{
int cnt = 0;
int val = p;
while ( true )
{
int a = r / val;
int b = (l - 1) / val;
val *= p;
if ((a - b) > 0)
{
cnt += (a - b);
}
else
break ;
}
return cnt;
}
public static void Main(String[] args)
{
int l = 2, r = 8, p = 2;
Console.WriteLine(getCount(l, r, p));
}
}
|
Javascript
<script>
function getCount(l, r, p)
{
let cnt = 0;
let val = p;
while (1) {
let a = parseInt(r / val);
let b = parseInt((l - 1) / val);
val *= p;
if (a - b) {
cnt += (a - b);
}
else
break ;
}
return cnt;
}
let l = 2, r = 8, p = 2;
document.write(getCount(l, r, p));
</script>
|
Time complexity: O(log(r) * log(r))
Auxiliary Space: O(1)
Last Updated :
05 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...