📜  计算给定范围内每个元素的素数分解中素数的出现

📅  最后修改于: 2021-06-27 01:13:54             🧑  作者: Mango

给定三个整数LRP ,其中P为质数,任务是计算范围[L,R]中所有数的质数分解中P发生的次数。
例子:

天真的方法:简单地遍历该范围,并为每个值计数P将该值除以多少次并将其求和得出结果。时间复杂度O(R – L)
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the highest
// power of p that divides n
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
// appaers 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 code
int main()
{
    int l = 2, r = 8, p = 2;
 
    cout << getCount(l, r, p);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
// Function to return the highest
// power of p that divides n
static 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
// appaers 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 code
public 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
# Python3 implementation of the approach
 
# Function to return the highest
# power of p that divides n
def 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
# appaers 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 code
if __name__ == "__main__" :
 
    l = 2; r = 8; p = 2;
 
    print(getCount(l, r, p));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the highest
// power of p that divides n
static 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
// appaers 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 code
public static void Main(String[] args)
{
    int l = 2, r = 8, p = 2;
 
    Console.WriteLine(getCount(l, r, p));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of times p
// appaers in the prime factors of the
// elements from the range [l, r]
int getCount(int l, int r, int p)
{
 
    // To store the requried 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 code
int main()
{
    int l = 2, r = 8, p = 2;
 
    cout << getCount(l, r, p);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the count of times p
// appaers in the prime factors of the
// elements from the range [l, r]
static int getCount(int l, int r, int p)
{
 
    // To store the requried 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 code
public 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
# Python3 implementation of the approach
 
# Function to return the count of times p
# appaers in the prime factors of the
# elements from the range [l, r]
def getCount(l, r, p):
 
    # To store the requried 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 Code
l = 2;
r = 8;
p = 2;
 
print(getCount(l, r, p));
 
# This code is contributed by princiraj


C#
// C# implementation of the approach
using System;
     
class GFG
{
 
// Function to return the count of times p
// appaers in the prime factors of the
// elements from the range [l, r]
static int getCount(int l, int r, int p)
{
 
    // To store the requried 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 code
public static void Main(String[] args)
{
    int l = 2, r = 8, p = 2;
 
    Console.WriteLine(getCount(l, r, p));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
7

有效方法:计算在[L,R]范围内被P,P 2 ,P 3 ,…,P x整除的值,其中xP的最大幂,以使P x处在上限( P x ≤N )。每次迭代花费O(1)时间,因此时间复杂度变为O(x) ,其中x =(log(R)/ log(P))
下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of times p
// appaers in the prime factors of the
// elements from the range [l, r]
int getCount(int l, int r, int p)
{
 
    // To store the requried 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 code
int main()
{
    int l = 2, r = 8, p = 2;
 
    cout << getCount(l, r, p);
 
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the count of times p
// appaers in the prime factors of the
// elements from the range [l, r]
static int getCount(int l, int r, int p)
{
 
    // To store the requried 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 code
public 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

# Python3 implementation of the approach
 
# Function to return the count of times p
# appaers in the prime factors of the
# elements from the range [l, r]
def getCount(l, r, p):
 
    # To store the requried 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 Code
l = 2;
r = 8;
p = 2;
 
print(getCount(l, r, p));
 
# This code is contributed by princiraj

C#

// C# implementation of the approach
using System;
     
class GFG
{
 
// Function to return the count of times p
// appaers in the prime factors of the
// elements from the range [l, r]
static int getCount(int l, int r, int p)
{
 
    // To store the requried 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 code
public static void Main(String[] args)
{
    int l = 2, r = 8, p = 2;
 
    Console.WriteLine(getCount(l, r, p));
}
}
 
// This code is contributed by PrinciRaj1992

Java脚本


输出:
7

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。