📜  用质数和计算子数组

📅  最后修改于: 2021-10-25 11:17:48             🧑  作者: Mango

给定一个整数数组 A[]。任务是计算总和为素数( size > 1 )的子数组总数。

例子

Input : A[] = { 1, 2, 3, 4, 5 }
Output : 3
Subarrays are -> {1, 2}, {2, 3}, {3, 4}

Input : A = { 22, 33, 4, 1, 10 };
Output : 4

方法:生成所有可能的子数组并将它们的和存储在一个向量中。迭代向量并检查和是否为素数。它是增加计数。
您可以使用 Sieve-of-Eratosthenes 检查总和是否为 O(1) 中的素数。

下面是上述方法的实现:

C++
// C++ program to count subarrays
// with Prime sum
 
#include 
using namespace std;
 
// Function to count subarrays
// with Prime sum
int primeSubarrays(int A[], int n)
{
    int max_val = int(pow(10, 7));
 
    // USE SIEVE TO FIND ALL PRIME NUMBERS LESS
    // THAN OR EQUAL TO max_val
    // Create a boolean array "prime[0..n]". A
    // value in prime[i] will finally be false
    // if i is Not a prime, else true.
    vector prime(max_val + 1, true);
 
    // Remaining part of SIEVE
    prime[0] = false;
    prime[1] = false;
    for (int p = 2; p * p <= max_val; p++) {
 
        // If prime[p] is not changed, then
        // it is a prime
        if (prime[p] == true) {
 
            // Update all multiples of p
            for (int i = p * 2; i <= max_val; i += p)
                prime[i] = false;
        }
    }
 
    int cnt = 0; // Initialize result
 
    // Traverse through the array
    for (int i = 0; i < n - 1; ++i) {
        int val = A[i];
        for (int j = i + 1; j < n; ++j) {
            val += A[j];
 
            if (prime[val])
                ++cnt;
        }
    }
 
    // return answer
    return cnt;
}
 
// Driver program
int main()
{
    int A[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(A) / sizeof(A[0]);
 
    cout << primeSubarrays(A, n);
 
    return 0;
}
Java // Java program to count subarrays 
// with Prime sum 
import java.util.*;
class Solution
{
  
// Function to count subarrays 
// with Prime sum 
static int primeSubarrays(int A[], int n) 
{ 
    int max_val = (int)(Math.pow(10, 7)); 
  
    // USE SIEVE TO FIND ALL PRIME NUMBERS LESS 
    // THAN OR EQUAL TO max_val 
    // Create a boolean array "prime[0..n]". A 
    // value in prime[i] will finally be false 
    // if i is Not a prime, else true. 
    Vector prime=new Vector(max_val + 1); 

    
    //initialize initial value
    for (int p = 0; p 


Python3
# Python3 program to count subarrays
# with Prime sum
 
# Function to count subarrays
# with Prime sum
def primeSubarrays(A, n):
 
    max_val = 10**7
 
    # USE SIEVE TO FIND ALL PRIME NUMBERS
    # LESS THAN OR EQUAL TO max_val
    # Create a boolean array "prime[0..n]". A
    # value in prime[i] will finally be false
    # if i is Not a prime, else true.
    prime = [True] * (max_val + 1)
 
    # Remaining part of SIEVE
    prime[0] = False
    prime[1] = False
    for p in range(2, int(max_val**(0.5)) + 1):
 
        # If prime[p] is not changed, then
        # it is a prime
        if prime[p] == True:
 
            # Update all multiples of p
            for i in range(2 * p, max_val + 1, p):
                prime[i] = False
         
    cnt = 0 # Initialize result
 
    # Traverse through the array
    for i in range(0, n - 1):
        val = A[i]
        for j in range(i + 1, n):
            val += A[j]
 
            if prime[val] == True:
                cnt += 1
 
    # return answer
    return cnt
 
# Driver Code
if __name__ == "__main__":
 
    A = [1, 2, 3, 4, 5]
    n = len(A)
 
    print(primeSubarrays(A, n))
 
# This code is contributed by Rituraj Jain


C#
// C# program to count subarrays
// with Prime sum
 
class Solution
{
 
// Function to count subarrays
// with Prime sum
static int primeSubarrays(int[] A, int n)
{
    int max_val = (int)(System.Math.Pow(10, 7));
 
    // USE SIEVE TO FIND ALL PRIME NUMBERS LESS
    // THAN OR EQUAL TO max_val
    // Create a boolean array "prime[0..n]". A
    // value in prime[i] will finally be false
    // if i is Not a prime, else true.
    bool[] prime=new bool[max_val + 1];
 
     
    //initialize initial value
    for (int p = 0; p 


PHP


Javascript


输出:
3

时间复杂度: O(N 2 )

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程