Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 06 / sieveOfEratosthenes.cpp
blob: 862fe75521e2692cfe3f2a7aa7b9d9b85f011059 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;

void init(bool arr[], unsigned size, bool value)
{
	for (unsigned i = 0; i < size; i++)
		arr[i] = value;
}

void sieveEratosthenes(bool arr[], unsigned size)
{
	arr[0] = arr[1] = false;

	for (unsigned i = 2; i < size; i++)
	{
		if (!arr[i]) //ако числото е задраскано
			continue;
		
		for (unsigned iter = i + i; iter < size; iter += i)
			arr[iter] = false; //задраскваме
	}
}

void printSieve(const bool arr[], unsigned size)
{
	for (unsigned i = 0; i < size; i++)
	{
		if (arr[i])
			cout << i << " ";
	}
}


int main()
{
	const unsigned MAX_SIZE = 1000;
	bool arr[MAX_SIZE];

	int n;
	cin >> n;

	init(arr, n, true);
	sieveEratosthenes(arr, n);
	printSieve(arr, n);
}