-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes.cpp
More file actions
46 lines (40 loc) · 821 Bytes
/
Copy pathprimes.cpp
File metadata and controls
46 lines (40 loc) · 821 Bytes
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>
#include <vector>
void find_primes(std::vector<int>& p,int max)
{
p.push_back(2);
for(int i = 3 ; i <= max;i++)
{
for(unsigned int j = 0; j <p.size(); j++)
{
if((i%p[j]) == 0)
{
p.push_back(i);
}
}
}
}
void print_primes(std::vector<int> p)
{
for(unsigned int i = 0; i < p.size();i++)
{
std::cout << "primes[" << i << "] = " << p[i] << "\n";
}
}
int main()
{
std::vector<int> primes;
std::cout << "This program calculates primes from 1 to max, please enter the value for max" << std::endl;
int max = 0;
if(std::cin >> max)
{
find_primes(primes,max);
print_primes(primes);
}
else
{
std::cout << "Error" << std::endl;
return -1;
}
return 0;
}