给定一个由 n 个正整数组成的数组 arr[]。任务是编写一个程序来寻找给定数组的所有素数的乘积。 例 :
输入 : arr[] = {1,3,4,5,7} 输出 : 105 有三个素数,3,5,7 的乘积= 105。 输入 : arr[] = {1,2,3,4,5,6,7} 输出 : 210
天真方法:一个简单的解决方法是遍历数组,不断检查每个元素是否是素数,同时计算素数元素的乘积。 高效方法:使用厄拉多塞的筛生成数组中最大元素的所有素数,并将它们存储在哈希中。现在遍历数组,用筛子找出那些质数的乘积。 以下是上述方法的实施:
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
python 3
# 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 */
服务器端编程语言(professional hypertext preprocessor 的缩写)
java 描述语言
output:
210
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处