数组中所有素数的乘积
给定一个包含 N 个正整数的数组 arr[]。任务是编写一个程序来找到给定数组的所有素数的乘积。
例子:
Input: arr[] = {1, 3, 4, 5, 7}
Output: 105
There are three primes, 3, 5 and 7 whose product = 105.
Input: arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 210
朴素方法:一个简单的解决方案是遍历数组并不断检查每个元素是否为素数,同时计算素数元素的乘积。
有效方法:使用 Eratosthenes 的筛子生成直到数组最大元素的所有素数,并将它们存储在哈希中。现在遍历数组并使用筛子找到那些素数元素的乘积。
下面是上述方法的实现:
C++
// CPP program to find product of
// primes in given array.
#include
using namespace std;
// Function to find the product of prime numbers
// in the given array
int primeProduct(int arr[], int n)
{
// Find maximum value in the array
int max_val = *max_element(arr, arr + n);
// 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;
}
}
// Product all primes in arr[]
int prod = 1;
for (int i = 0; i < n; i++)
if (prime[arr[i]])
prod *= arr[i];
return prod;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << primeProduct(arr, n);
return 0;
}
Java
// Java program to find product of
// primes in given array.
import java.util.*;
class GFG
{
// Function to find the product of prime numbers
// in the given array
static int primeProduct(int arr[], int n)
{
// Find maximum value in the array
int max_val = Arrays.stream(arr).max().getAsInt();
// 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);
for(int i = 0; i < max_val + 1; i++)
prime.add(i, Boolean.TRUE);
// Remaining part of SIEVE
prime.add(0, Boolean.FALSE);
prime.add(1, Boolean.FALSE);
for (int p = 2; p * p <= max_val; p++)
{
// If prime[p] is not changed, then
// it is a prime
if (prime.get(p) == true)
{
// Update all multiples of p
for (int i = p * 2; i <= max_val; i += p)
prime.add(i, Boolean.FALSE);
}
}
// Product all primes in arr[]
int prod = 1;
for (int i = 0; i < n; i++)
if (prime.get(arr[i]))
prod *= arr[i];
return prod;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int n = arr.length;
System.out.print(primeProduct(arr, n));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 program to find product of
# primes in given array
import math as mt
# function to find the product of prime
# numbers in the given array
def primeProduct(arr, n):
# find the maximum value in the array
max_val = max(arr)
# 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 for i in range(max_val + 1)]
# remaining part of SIEVE
prime[0] = False
prime[1] = False
for p in range(mt.ceil(mt.sqrt(max_val))):
# Remaining part of SIEVE
# if prime[p] is not changed,
# than it is prime
if prime[p]:
# update all multiples of p
for i in range(p * 2, max_val + 1, p):
prime[i] = False
# product all primes in arr[]
prod = 1
for i in range(n):
if prime[arr[i]]:
prod *= arr[i]
return prod
# Driver code
arr = [1, 2, 3, 4, 5, 6, 7]
n = len(arr)
print(primeProduct(arr, n))
# This code is contributed
# by Mohit kumar 29
C#
// C# program to find product of
// primes in given array.
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
// Function to find the product of prime numbers
// in the given array
static int primeProduct(int []arr, int n)
{
// Find maximum value in the array
int max_val = arr.Max();
// 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.
List prime = new List(max_val + 1);
for(int i = 0; i < max_val + 1; i++)
prime.Insert(i, true);
// Remaining part of SIEVE
prime.Insert(0, false);
prime.Insert(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.Insert(i, false);
}
}
// Product all primes in arr[]
int prod = 1;
for (int i = 0; i < n; i++)
if (prime[arr[i]])
prod *= arr[i];
return prod;
}
// Driver code
public static void Main()
{
int []arr = { 1, 2, 3, 4, 5, 6, 7 };
int n = arr.Length;
Console.Write(primeProduct(arr, n));
}
}
/* This code contributed by PrinciRaj1992 */
PHP
Javascript
输出:
210