给定一个整数数组。任务是计算一个数组中所有复合数的乘积。 注: 1 既不是素数,也不是复合数。 示例:
input: arr[] = {2, 3, 4, 5, 6, 7}
output: 24
composite numbers are 4 and 6\.
so, product = 24
input: arr[] = {11, 13, 17, 20, 19}
output: 20
天真方法:一个简单的pg电子试玩链接的解决方案是遍历数组,对每个元素做素性测试。如果元素不是素数也不是 1,将其乘以运行乘积。 时间复杂度–o(nsqrt(n)) 高效方法:使用厄拉多塞筛生成一个布尔向量,该向量达到数组中最大元素的大小,可用于检查一个数是否为素数。另外,将 0 和 1 相加作为质数,这样它们就不会被算作复合数。现在遍历数组,使用生成的布尔向量找到那些复合元素的乘积。
c
// c program to find the product
// of all the composite numbers
// in an array
#include
using namespace std;
// function that returns the
// the product of all composite numbers
int compositeproduct(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);
// set 0 and 1 as primes as
// they don't need to be
// counted as composite numbers
prime[0] = true;
prime[1] = true;
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;
}
}
// find the product of all
// composite numbers in the arr[]
int product = 1;
for (int i = 0; i < n; i )
if (!prime[arr[i]]) {
product *= arr[i];
}
return product;
}
// driver code
int main()
{
int arr[] = { 2, 3, 4, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << compositeproduct(arr, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to find the product
// of all the composite numbers
// in an array
import java.util.*;
class gfg {
// function that returns the
// the product of all composite numbers
static int compositeproduct(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.
boolean[] prime = new boolean[max_val 1];
arrays.fill(prime, true);
// set 0 and 1 as primes as
// they don't need to be
// counted as composite numbers
prime[0] = true;
prime[1] = true;
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;
}
}
}
// find the product of all
// composite numbers in the arr[]
int product = 1;
for (int i = 0; i < n; i ) {
if (!prime[arr[i]]) {
product *= arr[i];
}
}
return product;
}
// driver code
public static void main(string[] args)
{
int arr[] = { 2, 3, 4, 5, 6, 7 };
int n = arr.length;
system.out.println(compositeproduct(arr, n));
}
}
// this code has been contributed by 29ajaykumar
python 3
'''
python3 program to find product of
all the composite numberes in given array'''
import math as mt
'''
function to find the product of all composite
niumbers in the given array
'''
def compositeproduct(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)]
'''
set 0 and 1 as primes as
they don't need to be
counted as composite numbers
'''
prime[0]= true
prime[1]= true
for p in range(2, 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
# find the product of all composite numbers in the arr[]
product = 1
for i in range(n):
if prime[arr[i]]== false:
product*= arr[i]
return product
# driver code
arr =[2, 3, 4, 5, 6, 7]
n = len(arr)
print(compositeproduct(arr, n))
# contributed by mohit kumar 29
c
// c# program to find the product
// of all the composite numbers
// in an array
using system;
using system.linq;
public class gfg {
// function that returns the
// the product of all composite numbers
static int compositeproduct(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.
bool[] prime = new bool[max_val 1];
for (int i = 0; i < max_val 1; i )
prime[i] = true;
// set 0 and 1 as primes as
// they don't need to be
// counted as composite numbers
prime[0] = true;
prime[1] = true;
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;
}
}
}
// find the product of all
// composite numbers in the arr[]
int product = 1;
for (int i = 0; i < n; i ) {
if (!prime[arr[i]]) {
product *= arr[i];
}
}
return product;
}
// driver code
public static void main()
{
int[] arr = { 2, 3, 4, 5, 6, 7 };
int n = arr.length;
console.writeline(compositeproduct(arr, n));
}
}
/* this code contributed by princiraj1992 */
服务器端编程语言(professional hypertext preprocessor 的缩写)
java 描述语言
output:
24
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处