子数组和是否为素数
给定一个数组和限制(下限和上限),检查给定限制中子数组的总和是否为素数
例子 :
Input : a[] = {1, 2, 3, 5, 5, 4, 7, 8, 9};
lower = 3, upper = 6
Output : Yes
Explanation:- subarray is {3, 5, 5, 4} and
sum of subarray 3+5+5+4 = 17 which is prime, so
the output is yes
Input : a[] = {1, 6, 4, 5, 5, 4, 7, 8, 9};
lower = 2, upper = 5
Output : No
Explanation:- subarray is {6, 4, 5, 5} and sum
of subarray 6+4+5+5 = 20 which is Not prime so the
output is No
- 首先使用上限和下限计算子数组的总和
- 然后检查总和是否为素数。
- 如果是素数则返回真,否则返回假。
让我们使用下面的代码来理解这种方法。
C++
// A C++ program to check the given
// subarray is prime or not
#include
using namespace std;
// function to check the array
bool isPrime(int a[], int lower,
int upper)
{
int n = 0;
// Calculate the sum of
// the subarray
for (int i = lower - 1;
i <= upper - 1;
i++)
n += a[i];
// check the sum of the
// subarray is prime or
// not (Corner case)
if (n <= 1)
return false;
// Check from 2 to n - 1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
// Driver Code
int main()
{
// taking input
int a[] = { 1, 2, 3, 5, 5,
4, 7, 8, 9 };
int lower = 3, upper = 6;
if (isPrime(a, lower, upper))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
Java
// A java program to check the given
// subarray is prime or not
import java.io.*;
public class GFG {
// function to check the array
static boolean isPrime(int a[],
int lower,
int upper)
{
int n = 0;
// Calculate the sum of
// the subarray
for (int i = lower - 1;
i <= upper - 1; i++)
n += a[i];
// check the sum of the
// subarray is prime or
// not (Corner case)
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
// Driver code
public static void main(String[] args)
{
// taking input
int a[] = { 1, 2, 3, 5, 5, 4, 7, 8, 9 };
int lower = 3, upper = 6;
if (isPrime(a, lower, upper))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Sam007.
Python3
# A Python3 program to check the given
# subarray is prime or not
# function to check the array
def isPrime(a, lower, upper) :
n = 0
# Calculate the sum of
# the subarray
for i in range(lower - 1, upper) :
n = n + a[i]
# check the sum of the
# subarray is prime or
# not (Corner case)
if (n <= 1) :
return False
# Check from 2 to n - 1
for i in range(2, n) :
if (n % i == 0) :
return False
return True
# Driver Code
# taking input
a = [1, 2, 3, 5, 5, 4, 7, 8, 9]
lower = 3
upper = 6
if (isPrime(a, lower, upper)) :
print ("Yes")
else :
print ("No")
# This code is contributed by
# Manish Shaw (manishshaw1)
C#
// A C# program to check the given
// subarray is prime or not
using System;
class GFG {
// function to check the array
static bool isPrime(int[] a,
int lower,
int upper)
{
int n = 0;
// Calculate the sum of
// the subarray
for (int i = lower - 1;
i <= upper - 1;
i++)
n += a[i];
// check the sum of the
// subarray is prime or
// not (Corner case)
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
// Driver Code
public static void Main()
{
// taking input
int[] a = { 1, 2, 3, 5, 5,
4, 7, 8, 9 };
int lower = 3, upper = 6;
if (isPrime(a, lower, upper))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Sam007
PHP
Javascript
输出:
Yes