Course Computational Mathematics

Sieve of Eratosthenes

Course's Page 14 min Text by
User Image
Yan Silva

In this lesson, we'll learn about the Sieve of Eratosthenes and some of its applications. We will solve the following base problem:

Given two numbers n,qn, q and a sequence vv of qq integers (2vin)(2 \leqslant v_i \leqslant n), for every number in this sequence, you need to say whether it is prime or not

As we saw in the previous lesson, we can answer whether vv is prime in O(n)O(\sqrt n). Then using the method we already know, our complexity will be O(qn)O(q\sqrt n).

#include <iostream>
using namespace std;

int main()
{
	int q, n;
	cin >> q >> n;

	for (int i = 0; i < q; i++)
	{
		int v;
		cin >> v;