📜  小于或等于N的半素数之和

📅  最后修改于: 2021-04-21 23:48:07             🧑  作者: Mango

给定整数N ,任务是找到小于或等于N的半素数之和。半质数是两个质数的倍数。

例子:

方法:

  1. 首先使用Sieve计算小于或等于N的素数,并将它们按排序顺序存储在向量中。
  2. 遍历素数的向量。修复其中一个素数,并开始使用此固定素数检查所有素数的乘积值。
  3. 由于素数是按排序顺序排列的,因此一旦我们找到积超过N的素数,那么所有剩余素数都将超过该素数。因此,在这里中断嵌套循环。
  4. 将所有有效对的乘积值添加到答案变量。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long int
  
// Vector to store the primes
vector pr;
  
// Create a boolean array "prime[0..n]"
bool prime[10000000 + 1];
void sieve(ll n)
{
  
    // Initialize all prime values to be true
    for (int i = 2; i <= n; i += 1) {
        prime[i] = 1;
    }
  
    for (ll p = 2; (ll)p * (ll)p <= n; p++) {
  
        // If prime[p] is not changed then it is a prime
        if (prime[p] == true) {
  
            // Update all multiples of p greater than or
            // equal to the square of it
            // numbers which are multiple of p and are
            // less than p^2 are already been marked
            for (ll i = (ll)p * (ll)p; i <= n; i += p)
                prime[i] = false;
        }
    }
  
    // Print all prime numbers
    for (ll p = 2; p <= n; p++)
        if (prime[p])
            pr.push_back(p);
}
  
// Function to return the semi-prime sum
ll SemiPrimeSum(ll N)
{
  
    // Variable to store the sum of semi-primes
    ll ans = 0;
  
    // Iterate over the prime values
    for (int i = 0; i < pr.size(); i += 1) {
  
        for (int j = i; j < pr.size(); j += 1) {
  
            // Break the loop once the product exceeds N
            if ((ll)pr[i] * (ll)pr[j] > N)
                break;
  
            // Add valid products which are less than
            // or equal to N
            // each product is a semi-prime number
            ans += (ll)pr[i] * (ll)pr[j];
        }
    }
    return ans;
}
  
// Driver code
int main()
{
  
    ll N = 6;
  
    sieve(N);
  
    cout << SemiPrimeSum(N);
  
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
  
class GFG
{
  
// Vector to store the primes
static Vector pr = new Vector<>();
  
// Create a boolean array "prime[0..n]"
static boolean prime[] = new boolean[10000000 + 1];
static void sieve(long n)
{
  
    // Initialize along prime values to be true
    for (int i = 2; i <= n; i += 1) 
    {
        prime[i] = true;
    }
  
    for (int p = 2; (int)p * (int)p <= n; p++)
    {
  
        // If prime[p] is not changed then it is a prime
        if (prime[p] == true)
        {
  
            // Update along multiples of p greater than or
            // equal to the square of it
            // numbers which are multiple of p and are
            // less than p^2 are already been marked
            for (int i = (int)p * (int)p; i <= n; i += p)
                prime[i] = false;
        }
    }
  
    // Print all prime numbers
    for (int p = 2; p <= n; p++)
        if (prime[p])
            pr.add((long)p);
}
  
// Function to return the semi-prime sum
static long SemiPrimeSum(long N)
{
  
    // Variable to store the sum of semi-primes
    long ans = 0;
  
    // Iterate over the prime values
    for (int i = 0; i < pr.size(); i += 1) 
    {
  
        for (int j = i; j < pr.size(); j += 1) 
        {
  
            // Break the loop once the product exceeds N
            if ((long)pr.get(i) * (long)pr.get(j) > N)
                break;
  
            // Add valid products which are less than
            // or equal to N
            // each product is a semi-prime number
            ans += (long)pr.get(i) * (long)pr.get(j);
        }
    }
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    long N = 6;
  
    sieve(N);
  
    System.out.println(SemiPrimeSum(N));
}
}
  
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the approach
  
# Vector to store the primes
pr=[]
  
# Create a boolean array "prime[0..n]"
prime = [1 for i in range(10000000 + 1)]
def sieve(n):
  
  
    for p in range(2, n):
  
        if p * p > n:
            break
  
        # If prime[p] is not changed then it is a prime
        if (prime[p] == True):
  
            # Update amultiples of p greater than or
            # equal to the square of it
            # numbers which are multiple of p and are
            # less than p^2 are already been marked
            for i in range(2 * p, n + 1, p):
                prime[i] = False
      
  
    # Praprime numbers
    for p in range(2, n + 1):
        if (prime[p]):
            pr.append(p)
  
  
# Function to return the semi-prime sum
def SemiPrimeSum(N):
  
    # Variable to store the sum of semi-primes
    ans = 0
  
    # Iterate over the prime values
    for i in range(len(pr)):
  
        for j in range(i,len(pr)):
  
            # Break the loop once the product exceeds N
            if (pr[i] * pr[j] > N):
                break
  
            # Add valid products which are less than
            # or equal to N
            # each product is a semi-prime number
            ans += pr[i] * pr[j]
      
    return ans
  
# Driver code
N = 6
  
sieve(N)
  
print(SemiPrimeSum(N))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
  
class GFG
{
  
// Vector to store the primes
static List pr = new List();
  
// Create a boolean array "prime[0..n]"
static bool []prime = new bool[10000000 + 1];
static void sieve(long n)
{
  
    // Initialize along prime values to be true
    for (int i = 2; i <= n; i += 1) 
    {
        prime[i] = true;
    }
  
    for (int p = 2; (int)p * (int)p <= n; p++)
    {
  
        // If prime[p] is not changed then it is a prime
        if (prime[p] == true)
        {
  
            // Update along multiples of p greater than or
            // equal to the square of it
            // numbers which are multiple of p and are
            // less than p^2 are already been marked
            for (int i = (int)p * (int)p; i <= n; i += p)
                prime[i] = false;
        }
    }
  
    // Print all prime numbers
    for (int p = 2; p <= n; p++)
        if (prime[p])
            pr.Add((long)p);
}
  
// Function to return the semi-prime sum
static long SemiPrimeSum(long N)
{
  
    // Variable to store the sum of semi-primes
    long ans = 0;
  
    // Iterate over the prime values
    for (int i = 0; i < pr.Count; i += 1) 
    {
  
        for (int j = i; j < pr.Count; j += 1) 
        {
  
            // Break the loop once the product exceeds N
            if ((long)pr[i] * (long)pr[j] > N)
                break;
  
            // Add valid products which are less than
            // or equal to N
            // each product is a semi-prime number
            ans += (long)pr[i] * (long)pr[j];
        }
    }
    return ans;
}
  
// Driver code
public static void Main(String[] args)
{
    long N = 6;
  
    sieve(N);
  
    Console.WriteLine(SemiPrimeSum(N));
}
}
  
// This code is contributed by Rajput-Ji


输出:
10