4739. Sieve of Eratosthenes

 

Given the values of a and b, print all primes in the interval from a to b inclusively.

 

Input. Two integers a and b (1 ≤ a ≤ b ≤ 100000).

 

Output. Print in one line all prime numbers in the interval from a to b inclusively.

 

Sample input

Sample output

1 10

2 3 5 7

 

 

SOLUTION

sieve of Eratosthenes

 

Algorithm analysis

Using the algorithm Sieve of Eratosthenes, fill the primes array, where

·        primes[i] = 1, if i is prime;

·        primes[i] = 0, if i is composite;

Print all numbers i in the interval from a to b, for which primes[i] = 1.

 

Example

The filled array primes  has the form:

 

The prime numbers in the interval [1; 10] are 2, 3, 5, 7.

 

Algorithm realization

Declare the working array primes.

 

#define MAX 100001

int primes[MAX];

 

The gen_primes function implements the Sieve of Eratosthenes and fills the primes array. The numbers 0 and 1 are not prime.

 

void gen_primes(void)

{

  int i, j;

  for (i = 0; i < MAX; i++) primes[i] = 1;

  primes[0] = primes[1] = 0;

  for (i = 2; i * i < MAX; i++)

    if (primes[i])

      for (j = i * i; j < MAX; j += i) primes[j] = 0;

}

 

The main part of the program. Fill the primes array.

 

gen_primes();

 

Read the input data.

 

scanf("%d %d", &a, &b);

 

Print all primes in the interval from a to b.

 

for (i = a; i <= b; i++)

  if (primes[i]) printf("%d ", i);

printf("\n");

 

Algorithm realization – bitset

Let’s declare a variable primes of type bitset to store information about prime numbers.

 

#define MAX 100001

bitset<MAX> primes;

 

The gen_primes function implements the Sieve of Eratosthenes. We fill the primes bitset. The numbers 0 and 1 are not prime.

 

void gen_primes(void)

{

  primes.flip(); // set all to 1

  primes.reset(0); primes.reset(1);

  for (int i = 2; i <= sqrt(MAX); i++)

    if (primes.test(i))

      for (int j = i * i; j < MAX; j += i) primes.reset(j);

}

 

The main part of the program. Fill the primes array.

 

gen_primes();

 

Read the input data.

 

scanf("%d %d", &a, &b);

 

Print all primes in the interval from a to b.

 

for (i = a; i <= b; i++)

  if (primes.test(i)) printf("%d ", i);

printf("\n");

 

Java realization

 

import java.util.*;

 

public class Main

{

  final static int MAX_SIZE = 100001;

  static BitSet bits = new BitSet(MAX_SIZE);

  

  public static void Eratosthenes()

  {

    bits.set(2, MAX_SIZE, true);

    for (int i = 2; i < Math.sqrt(MAX_SIZE); i++)

    {

      if (bits.get(i))

        for (int j = i * i; j < MAX_SIZE; j += i)

          bits.set(j, false);

    }

  }

 

  public static void main(String args[])

  {

    Scanner con = new Scanner(System.in);

    Eratosthenes();

    int a = con.nextInt();

    int b = con.nextInt();

    for (int i = a; i <= b; i++)

      if (bits.get(i)) System.out.print(i + " ");

    System.out.println();

    con.close();

  }

}

 

Python realization

The seive_of_eratosthenes function implements the Sieve of Eratosthenes and fills the is_prime list. The numbers 0 and 1 are not prime.

 

def seive_of_eratosthenes(n):

  is_prime = [True] * (n+1)

  is_prime[0], is_prime[1] = False, False

 

  for i in range(2, int(n ** 0.5) + 1):

    if is_prime[i]:

      for j in range(i * i, n + 1, i):

        is_prime[j] = False

 

  return is_prime

 

The main part of the program. Fill the prime list.

 

prime = seive_of_eratosthenes(100000)

 

Read the input data.

 

a, b = map(int, input().split())

 

Print all primes in the interval from a to b.

 

for i in range (a, b + 1):

  if prime[i]: print(i,end=' ')