Sieve of Eratosthenes

Lecture



It is likely that the algorithm, invented over 2000 years ago by the Greek mathematician Eratosthenes Kirensky, was the first of its kind. His only task is to find all primes up to some given number N. The term “sieve” means filtering, namely, filtering all numbers except prime numbers. Thus, the processing by the algorithm of a numerical sequence will leave only simple numbers, all the components will be eliminated.

Let us consider in general the work of the method. An ordered sequence of natural numbers is given. Following the method of Eratosthenes, we take a certain number P initially equal to 2 - the first prime number, and delete from the sequence all multiples of P: 2P, 3P, 4P, ..., iP (iP≤N). Further, from the resulting list, P is taken as the next number after the two - the triple, deletes all multiples of it (6, 9, 12, ...). According to this principle, the algorithm continues to run for the remainder of the sequence, sifting out all composite numbers in a given range.

2

3

four

five

6

7

eight

9

ten

eleven

12

13

14

15

sixteen

17

18

nineteen

20

21

22

23

24

25

26

27

28

29

thirty

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

The following table contains natural numbers from 2 to 100. Red marks those that are deleted during the execution of the “Sieve of Eratosthenes” algorithm.

Software implementation of the Eratosthenen algorithm will require:

  1. organize a logical array and assign it to the elements from the range from 2 to N logical unit;
  2. in the free variable P write the number 2, which is the first prime number;
  3. remove from the array all the multiples of P 2 , stepping in steps of P;
  4. write in P the following not crossed out number;
  5. repeat the steps described in the two previous paragraphs, as long as possible.

Note: in the third step, we exclude numbers, starting right away from P 2 , this is due to the fact that all the composite numbers less than P will already be crossed out. Therefore, the filtering process should be stopped when P 2 becomes greater than N. This important remark allows us to improve the algorithm by reducing the number of operations performed.

This is what the pseudo-code of the algorithm will look like:

P ← 2
while P 2 ≤N perform
{
i ← P 2
if B [P] = true then
until i≤N perform
{
B [i] ← false
i ← i + P
}
P ← P + 1
}

It consists of two cycles: external and internal. The outer loop is executed until P 2 exceeds N. The very same P changes with step P + 1. The inner loop is executed only if at the next step of the outer loop it turns out that the element with the index P is not crossed out. It is in the inner loop that all composite numbers are eliminated.

C ++ program code:

one
2
3
four
five
6
7
eight
9
ten
eleven
12
13
14
15
sixteen
17
18
nineteen
20
21
22
23
24
25
26
27
28
29
thirty
31
32
33
34
#include "stdafx.h"
#include
using namespace std;
// Sieve of Eratosthenes
void Eratosthenes (bool B [], int N)
{
int i, p;
for (P = 2; P <= N; P ++) B [P] = true;
P = 2;
while (P * P <= N)
{
i = P * P;
if (B [P])
while (i <= n)
{
B [i] = false;
i = i + P;
}
P = P + 1;
}
cout << "Prime numbers:";
for (P = 2; P <= N; P ++)
if (B [P] == true) cout << "" << P;
}
// main function
void main ()
{
setlocale (LC_ALL, "Rus");
int N;
cout << "N>"; cin >> N;
bool * B = new bool [N];
Eratosthenes (B, N);
system ("pause >> void");
}

Pascal program code:

one
2
3
four
five
6
7
eight
9
ten
eleven
12
13
14
15
sixteen
17
18
nineteen
20
21
22
23
24
25
26
27
28
29
thirty
31
32
program EratosthenesAlg;
uses crt;
type Arr = array [1..1000] of boolean;
var
N, i, P: integer;
B: Arr;
{sieve of Eratosthenes}
procedure Eratosthenes (B: Arr; N: integer);
begin
for P: = 2 to N do B [P]: = true;
P: = 2;
while (P * P <= N) do
begin
i: = P * P;
if B [P] then
while i <= N do
begin
B [i]: = false;
i: = i + P;
end;
P: = P + 1;
end;
write ('Prime numbers:');
for P: = 2 to n do
if B [P] = true then write (P, '');
end;
{main program block}
begin
clrscr;
write ('N>'); read (N);
Eratosthenes (B, N);
end.

The sieve of Eratosthenes to identify all primes in a given sequence limited to some N will require O ( N log (log N )) operations. Therefore, it is more appropriate to use this method than, for example, the most trivial and costly search for dividers.

created: 2014-11-30
updated: 2021-03-13
132742



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Algorithms

Terms: Algorithms