📌  相关文章
📜  计算最多N个可以表示为质数幂的数字

📅  最后修改于: 2021-04-18 02:39:38             🧑  作者: Mango

给定一个整数N ,任务是要计算范围[1,N]的数字它们是质数的幂。

例子:

方法:可以使用Eratosthenes筛子解决问题。

  1. 使用Eratosthenes的Sieve初始化长度为N + 1的数组prime [] ,其中prime [i] = 1表示i是素数, prime [i] = 0表示i不是素数。
  2. 将所有质数推入向量,例如v
  3. 初始化一个变量,例如ans,以存储素数幂的计数。
  4. 对于每个素数,在向量v中p ,执行以下操作:
    • 初始化一个等于p的变量,例如temp
    • 检查温度是否小于N。如果发现温度为真,则执行以下操作:
      • ans增加1
      • 更新TEMP =临时* P,P的下一个动力。
  5. 返回最终计数为ans

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number
// of powers of prime numbers upto N
int countPowerOfPrimes(int N)
{
 
    // Sieve array
    int prime[N + 1];
 
    // Sieve of Eratosthenes
 
    // Initialize all numbers as prime
    for (int i = 0; i <= N; i++)
        prime[i] = 1;
 
    // Mark 0 and 1 as non prime
    prime[0] = 0;
    prime[1] = 0;
 
    for (int i = 2; i * i <= N; i++) {
        // If a prime number is found
        if (prime[i] == 1) {
            // Mark all multiples
            // of i as non-prime
            for (int j = i * i;
                 j <= N; j += i) {
                prime[j] = 0;
            }
        }
    }
 
    // Stores all prime
    // numbers upto N
    vector v;
 
    // Push all the primes into v
    for (int i = 2; i <= N; i++) {
        if (prime[i] == 1) {
            v.push_back(i);
        }
    }
 
    // Stores the count of
    // powers of primes up to N
    int ans = 0;
 
    // Iteratre over every
    // prime number up to N
    for (auto p : v) {
        // Store p in temp
        int temp = p;
 
        // Iterate until temp exceeds n
        while (temp <= N) {
            // Increment ans by 1
            ans = ans + 1;
 
            // Update temp to
            // next power of p
            temp = temp * p;
        }
    }
 
    // Return ans as the final answer
    return ans;
}
 
// Driver Code
int main()
{
    // Given Input
    int n = 9;
 
    // Function call to count
    // the number of power of
    // primes in the range [1, N]
    cout << countPowerOfPrimes(n);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to count the number
// of powers of prime numbers upto N
static int countPowerOfPrimes(int N)
{
     
    // Sieve array
    int prime[] = new int[N + 1];
 
    // Sieve of Eratosthenes
 
    // Initialize all numbers as prime
    for(int i = 0; i <= N; i++)
        prime[i] = 1;
 
    // Mark 0 and 1 as non prime
    prime[0] = 0;
    prime[1] = 0;
 
    for(int i = 2; i * i <= N; i++)
    {
         
        // If a prime number is found
        if (prime[i] == 1)
        {
             
            // Mark all multiples
            // of i as non-prime
            for(int j = i * i;
                    j < N + 1;
                    j += i)
            {
                prime[j] = 0;
            }
        }
    }
     
    // Stores all prime
    // numbers upto N
    int v[] = new int[N + 1];
    int j = 0;
 
    // Push all the primes into v
    for(int i = 2; i < N + 1; i++)
    {
        if (prime[i] == 1)
        {
            v[j] = i;
            j += 1;
        }
    }
     
    // Stores the count of
    // powers of primes up to N
    int ans = 0;
 
    // Iteratre over every
    // prime number up to N
    for(int k = 0; k < j; k++)
    {
         
        // Store v[k] in temp
        int temp = v[k];
 
        // Iterate until temp exceeds n
        while (temp <= N)
        {
             
            // Increment ans by 1
            ans = ans + 1;
 
            // Update temp to
            // next power of v[k]
            temp = temp * v[k];
        }
    }
 
    // Return ans as the final answer
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 9;
     
    // Function call to count
    // the number of power of
    // primes in the range [1, N]
    System.out.println(countPowerOfPrimes(n));
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to count the number
# of powers of prime numbers upto N
def countPowerOfPrimes(N):
 
    # Sieve array
    prime = [1] * (N + 1)
 
    # Mark 0 and 1 as non prime
    prime[0] = 0
    prime[1] = 0
 
    for i in range(2, N + 1):
        if i * i > N:
            break
         
        # If a prime number is found
        if (prime[i] == 1):
             
            # Mark all multiples
            # of i as non-prime
            for j in range(i * i, N + 1, i):
                prime[j] = 0
 
    # Stores all prime
    # numbers upto N
    v = []
 
    # Push all the primes into v
    for i in range(2, N + 1):
        if (prime[i] == 1):
            v.append(i)
 
    # Stores the count of
    # powers of primes up to N
    ans = 0
 
    # Iteratre over every
    # prime number up to N
    for p in v:
         
        # Store p in temp
        temp = p
 
        # Iterate until temp exceeds n
        while (temp <= N):
             
            # Increment ans by 1
            ans = ans + 1
 
            # Update temp to
            # next power of p
            temp = temp * p
 
    # Return ans as the final answer
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    n = 9
 
    # Function call to count
    # the number of power of
    # primes in the range [1, N]
    print (countPowerOfPrimes(n))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the number
// of powers of prime numbers upto N
static int countPowerOfPrimes(int N)
{
     
    // Sieve array
    int[] prime = new int[N + 1];
    int j;
 
    // Sieve of Eratosthenes
 
    // Initialize all numbers as prime
    for(int i = 0; i <= N; i++)
        prime[i] = 1;
 
    // Mark 0 and 1 as non prime
    prime[0] = 0;
    prime[1] = 0;
 
    for(int i = 2; i * i <= N; i++)
    {
         
        // If a prime number is found
        if (prime[i] == 1)
        {
             
            // Mark all multiples
            // of i as non-prime
            for(j = i * i;
                j < N + 1;
                j += i)
            {
                prime[j] = 0;
            }
        }
    }
     
    // Stores all prime
    // numbers upto N
    int[] v = new int[N + 1];
    j = 0;
 
    // Push all the primes into v
    for(int i = 2; i < N + 1; i++)
    {
        if (prime[i] == 1)
        {
            v[j] = i;
            j += 1;
        }
    }
     
    // Stores the count of
    // powers of primes up to N
    int ans = 0;
 
    // Iteratre over every
    // prime number up to N
    for(int k = 0; k < j; k++)
    {
         
        // Store v[k] in temp
        int temp = v[k];
 
        // Iterate until temp exceeds n
        while (temp <= N)
        {
             
            // Increment ans by 1
            ans = ans + 1;
 
            // Update temp to
            // next power of v[k]
            temp = temp * v[k];
        }
    }
 
    // Return ans as the final answer
    return ans;
}
 
// Driver Code
public static void Main(string[] args)
{
    int n = 9;
     
    // Function call to count
    // the number of power of
    // primes in the range [1, N]
    Console.Write(countPowerOfPrimes(n));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
7

时间复杂度: O(N log(log N))
辅助空间: O(N)