Count occurrences of a prime number in the prime factorization of every element from the given range

18 Mar, 2022
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  
1 + 0 + 2 + 0 + 1 + 0 + 3 = 7Input: 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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <iostream>using namespace std; // Function to return the highest// power of p that divides nint countFactors(int n, int p){    int pwr = 0;    while (n > 0 && n % p == 0) {        n /= p;        pwr++;    }    return pwr;} // Function to return the count of times p// appears in the prime factors of the// elements from the range [l, r]int getCount(int l, int r, int p){     // To store the required count    int cnt = 0;     // For every element of the range    for (int i = l; i <= r; i++) {         // Add the highest power of        // p that divides i        cnt += countFactors(i, p);    }     return cnt;} // Driver codeint main(){    int l = 2, r = 8, p = 2;     cout << getCount(l, r, p);     return 0;}
// Java implementation of the approachclass GFG{ // Function to return the highest// power of p that divides nstatic int countFactors(int n, int p){    int pwr = 0;    while (n > 0 && n % p == 0)    {        n /= p;        pwr++;    }    return pwr;} // Function to return the count of times p// appears in the prime factors of the// elements from the range [l, r]static int getCount(int l, int r, int p){     // To store the required count    int cnt = 0;     // For every element of the range    for (int i = l; i <= r; i++)    {         // Add the highest power of        // p that divides i        cnt += countFactors(i, p);    }    return cnt;} // Driver codepublic static void main(String[] args){    int l = 2, r = 8, p = 2;     System.out.println(getCount(l, r, p));}} // This code is contributed by Rajput-Ji
# Python3 implementation of the approach # Function to return the highest# power of p that divides ndef countFactors(n, p) :     pwr = 0;         while (n > 0 and n % p == 0) :        n //= p;        pwr += 1;             return pwr; # Function to return the count of times p# appears in the prime factors of the# elements from the range [l, r]def getCount(l, r, p) :     # To store the required count    cnt = 0;     # For every element of the range    for i in range(l, r + 1) :         # Add the highest power of        # p that divides i        cnt += countFactors(i, p);     return cnt; # Driver codeif __name__ == "__main__" :     l = 2; r = 8; p = 2;     print(getCount(l, r, p)); # This code is contributed by AnkitRai01
// C# implementation of the approachusing System; class GFG{ // Function to return the highest// power of p that divides nstatic int countFactors(int n, int p){    int pwr = 0;    while (n > 0 && n % p == 0)    {        n /= p;        pwr++;    }    return pwr;} // Function to return the count of times p// appears in the prime factors of the// elements from the range [l, r]static int getCount(int l, int r, int p){     // To store the required count    int cnt = 0;     // For every element of the range    for (int i = l; i <= r; i++)    {         // Add the highest power of        // p that divides i        cnt += countFactors(i, p);    }    return cnt;} // Driver codepublic static void Main(String[] args){    int l = 2, r = 8, p = 2;     Console.WriteLine(getCount(l, r, p));}} // This code is contributed by 29AjayKumar
<script> // Javascript implementation of the approach // Function to return the highest// power of p that divides nfunction countFactors(n, p){    let pwr = 0;    while (n > 0 && n % p == 0) {        n /= p;        pwr++;    }    return pwr;} // Function to return the count of times p// appears in the prime factors of the// elements from the range [l, r]function getCount(l, r, p){     // To store the required count    let cnt = 0;     // For every element of the range    for (let i = l; i <= r; i++) {         // Add the highest power of        // p that divides i        cnt += countFactors(i, p);    }     return cnt;} // Driver code    let l = 2, r = 8, p = 2;     document.write(getCount(l, r, p)); </script>
7
 
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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <iostream>using namespace std; // Function to return the count of times p// appears in the prime factors of the// elements from the range [l, r]int getCount(int l, int r, int p){     // To store the required count    int cnt = 0;    int val = p;    while (1) {         // Number of values in the range [0, r]        // that are divisible by val        int a = r / val;         // Number of values in the range [0, l - 1]        // that are divisible by val        int b = (l - 1) / val;         // Increment the power of the val        val *= p;         // (a - b) is the count of numbers in the        // range [l, r] that are divisible by val        if (a - b) {            cnt += (a - b);        }         // No values that are divisible by val        // thus exiting from the loop        else            break;    }     return cnt;} // Driver codeint main(){    int l = 2, r = 8, p = 2;     cout << getCount(l, r, p);     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{ // Function to return the count of times p// appears in the prime factors of the// elements from the range [l, r]static int getCount(int l, int r, int p){     // To store the required count    int cnt = 0;    int val = p;    while (true)    {         // Number of values in the range [0, r]        // that are divisible by val        int a = r / val;         // Number of values in the range [0, l - 1]        // that are divisible by val        int b = (l - 1) / val;         // Increment the power of the val        val *= p;         // (a - b) is the count of numbers in the        // range [l, r] that are divisible by val        if ((a - b) > 0)        {            cnt += (a - b);        }         // No values that are divisible by val        // thus exiting from the loop        else            break;    }    return cnt;} // Driver codepublic static void main(String[] args){    int l = 2, r = 8, p = 2;     System.out.println(getCount(l, r, p));}} // This code is contributed by 29AjayKumar
# Python3 implementation of the approach # Function to return the count of times p# appears in the prime factors of the# elements from the range [l, r]def getCount(l, r, p):     # To store the required count    cnt = 0;    val = p;    while (True):         # Number of values in the range [0, r]        # that are divisible by val        a = r // val;         # Number of values in the range [0, l - 1]        # that are divisible by val        b = (l - 1) // val;         # Increment the power of the val        val *= p;         # (a - b) is the count of numbers in the        # range [l, r] that are divisible by val        if (a - b):            cnt += (a - b);         # No values that are divisible by val        # thus exiting from the loop        else:            break;     return int(cnt); # Driver Codel = 2;r = 8;p = 2; print(getCount(l, r, p)); # This code is contributed by princiraj
// C# implementation of the approachusing System;     class GFG{ // Function to return the count of times p// appears in the prime factors of the// elements from the range [l, r]static int getCount(int l, int r, int p){     // To store the required count    int cnt = 0;    int val = p;    while (true)    {         // Number of values in the range [0, r]        // that are divisible by val        int a = r / val;         // Number of values in the range [0, l - 1]        // that are divisible by val        int b = (l - 1) / val;         // Increment the power of the val        val *= p;         // (a - b) is the count of numbers in the        // range [l, r] that are divisible by val        if ((a - b) > 0)        {            cnt += (a - b);        }         // No values that are divisible by val        // thus exiting from the loop        else            break;    }    return cnt;} // Driver codepublic static void Main(String[] args){    int l = 2, r = 8, p = 2;     Console.WriteLine(getCount(l, r, p));}} // This code is contributed by PrinciRaj1992
<script>// Javascript implementation of the approach // Function to return the count of times p// appears in the prime factors of the// elements from the range [l, r]function getCount(l, r, p){     // To store the required count    let cnt = 0;    let val = p;    while (1) {         // Number of values in the range [0, r]        // that are divisible by val        let a = parseInt(r / val);         // Number of values in the range [0, l - 1]        // that are divisible by val        let b = parseInt((l - 1) / val);         // Increment the power of the val        val *= p;         // (a - b) is the count of numbers in the        // range [l, r] that are divisible by val        if (a - b) {            cnt += (a - b);        }         // No values that are divisible by val        // thus exiting from the loop        else            break;    }     return cnt;} // Driver code    let l = 2, r = 8, p = 2;     document.write(getCount(l, r, p)); </script>
7
 
Auxiliary Space: O(1)
ankthon
Rajput-Ji
29AjayKumar
princiraj1992
souravmahato348
rishavmahato348
gabaa406
manikarora059
abhishek0719kadiyan
subhamkumarm348
Prime Number
prime-factor
Competitive Programming
Mathematical
Mathematical
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.