给定一个整数“k”和一个整数数组“arr”(小于 10^6),任务是找出数组中每第 k 个素数的乘积。 例:
输入: arr = {2,3,5,7,11},k = 2 输出: 21 数组的所有元素都是质数。所以,每个 k(即 2)区间后的素数是 3,7,它们的乘积是 21。 输入: arr = {41,23,12,17,18,19},k = 2 输出: 437
一个简单的方法:遍历数组,找到数组中的每 k 个素数,计算运行乘积。这样,我们将不得不检查数组的每个元素是否是质数,随着数组大小的增加,这将花费更多的时间。 有效方法:创建一个筛子,用来存储一个数是否是质数。然后,它可以用来在 o(1)时间内检查一个数字是否符合质数。这样,我们只需要跟踪每个第 k 个素数,并维护运行的产品。 以下是上述方法的实施:
c
// c implementation of the approach
#include
using namespace std;
#define max 1000000
bool prime[max 1];
void sieveoferatosthenes()
{
// create a boolean array "prime[0..n]"
// and initialize all the entries as true.
// a value in prime[i] will finally be false
// if i is not a prime, else true.
memset(prime, true, sizeof(prime));
// 0 and 1 are not prime numbers
prime[1] = false;
prime[0] = false;
for (int p = 2; p * p <= max; 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; i = p)
prime[i] = false;
}
}
}
// compute the answer
void productofkthprimes(int arr[], int n, int k)
{
// count of primes
int c = 0;
// product of the primes
long long int product = 1;
// traverse the array
for (int i = 0; i < n; i ) {
// if the number is a prime
if (prime[arr[i]]) {
// increase the count
c ;
// if it is the k'th prime
if (c % k == 0) {
product *= arr[i];
c = 0;
}
}
}
cout << product << endl;
}
// driver code
int main()
{
// create the sieve
sieveoferatosthenes();
int n = 5, k = 2;
int arr[n] = { 2, 3, 5, 7, 11 };
productofkthprimes(arr, n, k);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java implementation of the approach
class gfg
{
static int max=1000000;
static boolean[] prime=new boolean[max 1];
static void sieveoferatosthenes()
{
// create a boolean array "prime[0..n]"
// and initialize all the entries as true.
// a value in prime[i] will finally be false
// if i is not a prime, else true.
//memset(prime, true, sizeof(prime));
// 0 and 1 are not prime numbers
prime[1] = true;
prime[0] = true;
for (int p = 2; p * p <= max; p ) {
// if prime[p] is not changed,
// then it is a prime
if (prime[p] == false) {
// update all multiples of p
for (int i = p * 2; i <= max; i = p)
prime[i] = true;
}
}
}
// compute the answer
static void productofkthprimes(int arr[], int n, int k)
{
// count of primes
int c = 0;
// product of the primes
int product = 1;
// traverse the array
for (int i = 0; i < n; i ) {
// if the number is a prime
if (!prime[arr[i]]) {
// increase the count
c ;
// if it is the k'th prime
if (c % k == 0) {
product *= arr[i];
c = 0;
}
}
}
system.out.println(product);
}
// driver code
public static void main(string[] args)
{
// create the sieve
sieveoferatosthenes();
int n = 5, k = 2;
int[] arr=new int[]{ 2, 3, 5, 7, 11 };
productofkthprimes(arr, n, k);
}
}
// this code is contributed by mits
python 3
# python 3 implementation of the approach
max = 1000000
prime = [true]*(max 1)
def sieveoferatosthenes():
# create a boolean array "prime[0..n]"
# and initialize all the entries as true.
# a value in prime[i] will finally be false
# if i is not a prime, else true.
# 0 and 1 are not prime numbers
prime[1] = false;
prime[0] = false;
p = 2
while p * p <= max:
# if prime[p] is not changed,
# then it is a prime
if (prime[p] == true):
# update all multiples of p
for i in range(p * 2, max 1, p):
prime[i] = false
p =1
# compute the answer
def productofkthprimes(arr, n, k):
# count of primes
c = 0
# product of the primes
product = 1
# traverse the array
for i in range( n):
# if the number is a prime
if (prime[arr[i]]):
# increase the count
c =1
# if it is the k'th prime
if (c % k == 0) :
product *= arr[i]
c = 0
print(product)
# driver code
if __name__ == "__main__":
# create the sieve
sieveoferatosthenes()
n = 5
k = 2
arr = [ 2, 3, 5, 7, 11 ]
productofkthprimes(arr, n, k)
# this code is contributed by chitranayal
c
// c# implementation of the approach
class gfg
{
static int max = 1000000;
static bool[] prime = new bool[max 1];
static void sieveoferatosthenes()
{
// create a boolean array "prime[0..n]"
// and initialize all the entries as
// true. a value in prime[i] will
// finally be false if i is not a prime,
// else true.
// 0 and 1 are not prime numbers
prime[1] = true;
prime[0] = true;
for (int p = 2; p * p <= max; p )
{
// if prime[p] is not changed,
// then it is a prime
if (prime[p] == false)
{
// update all multiples of p
for (int i = p * 2;
i <= max; i = p)
prime[i] = true;
}
}
}
// compute the answer
static void productofkthprimes(int[] arr,
int n, int k)
{
// count of primes
int c = 0;
// product of the primes
int product = 1;
// traverse the array
for (int i = 0; i < n; i )
{
// if the number is a prime
if (!prime[arr[i]])
{
// increase the count
c ;
// if it is the k'th prime
if (c % k == 0)
{
product *= arr[i];
c = 0;
}
}
}
system.console.writeline(product);
}
// driver code
static void main()
{
// create the sieve
sieveoferatosthenes();
int n = 5, k = 2;
int[] arr=new int[]{ 2, 3, 5, 7, 11 };
productofkthprimes(arr, n, k);
}
}
// this code is contributed by mits
java 描述语言
output:
21
时间复杂度: o(n)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处