给定范围[L,R],任务是从该范围中找到除数和质数都为偶数的数字。
然后,打印找到的数字计数。 L和R的值小于10 ^ 6并且L
Input: L=3, R=9
Output: Count = 3
Explanation: The numbers are 3, 5, 7
Input : L=3, R=17
Output : Count: 6
- The only number that is prime, as well as even, is ‘2’.
- So, we need to find all the numbers within the given range that have exactly 2 divisors,
i.e. prime numbers.
一种简单的方法:
- 从“ l”到“ r”开始循环,然后检查数字是否为质数(更大范围需要更多时间)。
- 如果数字是素数,则增加count变量。
- 最后,打印计数值。
一种有效的方法:
- 我们必须计算在[L,R]范围内的素数。
- 首先,创建一个筛子,这将有助于确定该数字在O(1)时间内是否为质数。
- 然后,创建一个前缀数组来存储素数的计数,其中索引“ i”处的元素保存从“ 1”到“ i”的素数的计数。
- 现在,如果要查找范围为[L,R]的素数计数,则计数为(sum [R] – sum [L-1])
- 最后,打印结果,即(sum [R] – sum [L-1])
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define MAX 1000000
// stores whether the number is prime or not
bool prime[MAX + 1];
// stores the count of prime numbers
// less than or equal to the index
int sum[MAX + 1];
// create the sieve
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));
memset(sum, 0, sizeof(sum));
prime[1] = false;
for (int p = 2; p * p <= MAX; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p]) {
// Update all multiples of p
for (int i = p * 2; i <= MAX; i += p)
prime[i] = false;
}
}
// stores the prefix sum of number
// of primes less than or equal to 'i'
for (int i = 1; i <= MAX; i++) {
if (prime[i] == true)
sum[i] = 1;
sum[i] += sum[i - 1];
}
}
// Driver code
int main()
{
// create the sieve
SieveOfEratosthenes();
// 'l' and 'r' are the lower and upper bounds
// of the range
int l = 3, r = 9;
// get the value of count
int c = (sum[r] - sum[l - 1]);
// display the count
cout << "Count: " << c << endl;
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static final int MAX=1000000;
// stores whether the number is prime or not
static boolean []prime=new boolean[MAX + 1];
// stores the count of prime numbers
// less than or equal to the index
static int []sum=new int[MAX + 1];
// create the sieve
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.
for(int i=0;i<=MAX;i++)
prime[i]=true;
for(int i=0;i<=MAX;i++)
sum[i]=0;
prime[1] = false;
for (int p = 2; p * p <= MAX; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p]) {
// Update all multiples of p
for (int i = p * 2; i <= MAX; i += p)
prime[i] = false;
}
}
// stores the prefix sum of number
// of primes less than or equal to 'i'
for (int i = 1; i <= MAX; i++) {
if (prime[i] == true)
sum[i] = 1;
sum[i] += sum[i - 1];
}
}
// Driver code
public static void main(String []args)
{
// create the sieve
SieveOfEratosthenes();
// 'l' and 'r' are the lower and upper bounds
// of the range
int l = 3, r = 9;
// get the value of count
int c = (sum[r] - sum[l - 1]);
// display the count
System.out.println("Count: " + c);
}
}
Python 3
# Python 3 implementation of the approach
MAX = 1000000
# stores whether the number is prime or not
prime = [True] * (MAX + 1)
# stores the count of prime numbers
# less than or equal to the index
sum = [0] * (MAX + 1)
# create the sieve
def SieveOfEratosthenes():
prime[1] = False
p = 2
while p * p <= MAX:
# If prime[p] is not changed,
# then it is a prime
if (prime[p]):
# Update all multiples of p
i = p * 2
while i <= MAX:
prime[i] = False
i += p
p += 1
# stores the prefix sum of number
# of primes less than or equal to 'i'
for i in range(1, MAX + 1):
if (prime[i] == True):
sum[i] = 1
sum[i] += sum[i - 1]
# Driver code
if __name__ == "__main__":
# create the sieve
SieveOfEratosthenes()
# 'l' and 'r' are the lower and
# upper bounds of the range
l = 3
r = 9
# get the value of count
c = (sum[r] - sum[l - 1])
# display the count
print("Count:", c)
# This code is contributed by ita_c
C#
// C# implementation of the approach
using System;
class GFG
{
static int MAX=1000000;
// stores whether the number is prime or not
static bool []prime=new bool[MAX + 1];
// stores the count of prime numbers
// less than or equal to the index
static int []sum=new int[MAX + 1];
// create the sieve
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.
for(int i=0;i<=MAX;i++)
prime[i]=true;
for(int i=0;i<=MAX;i++)
sum[i]=0;
prime[1] = false;
for (int p = 2; p * p <= MAX; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p]) {
// Update all multiples of p
for (int i = p * 2; i <= MAX; i += p)
prime[i] = false;
}
}
// stores the prefix sum of number
// of primes less than or equal to 'i'
for (int i = 1; i <= MAX; i++) {
if (prime[i] == true)
sum[i] = 1;
sum[i] += sum[i - 1];
}
}
// Driver code
public static void Main()
{
// create the sieve
SieveOfEratosthenes();
// 'l' and 'r' are the lower and upper bounds
// of the range
int l = 3, r = 9;
// get the value of count
int c = (sum[r] - sum[l - 1]);
// display the count
Console.WriteLine("Count: " + c);
}
}
PHP
Javascript
输出:
Count: 3
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。