Sieve of Eratosthenes#4739

Sieve of Eratosthenes (Editorial)

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

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

Example

The filled array has the form:

The prime numbers in the interval are 2, 3, 5, 7.

Algorithm implementation

Declare the working array .

#define MAX 100001
int primes[MAX];

The gen_primes function implements the Sieve of Eratosthenes and fills the array. The numbers and 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 array.

gen_primes();

Read the input data.

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

Print all primes in the interval from to .

for (i = a; i <= b; i++)
  if (primes[i]) printf("%d ", i);
printf("\n");

Algorithm implementation — bitset

Let's declare a variable 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 bitset. The numbers and 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 array.

gen_primes();

Read the input data.

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

Print all primes in the interval from to .

for (i = a; i <= b; i++)
  if (primes.test(i)) printf("%d ", i);
printf("\n");

Java implementation

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 implementation

The seive_of_eratosthenes function implements the Sieve of Eratosthenes and fills the is_prime list. The numbers and 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 list.

prime = seive_of_eratosthenes(100000)

Read the input data.

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

Print all primes in the interval from to .

for i in range (a, b + 1):
  if prime[i]: print(i,end=' ')