-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimes.cpp
39 lines (36 loc) · 945 Bytes
/
primes.cpp
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
// ID 10979076
// Clifford Anim Boateng
// I certify that, I Clifford Boateng, I wrote this code all by myself.
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
int number {0};
int main(){
cout << "Enter Number as range from 1 to 'n':";
cin >> number;
int sum{0};
vector<int> primes;
for(int i{2}; i <= number; i++)
{
bool prime{true};
for(int j{0};j<primes.size() && primes[j]*primes[j] <= i;j++)
{
if(i % primes[j] == 0)
{
prime=false;
break;
}
}
if(prime)
{
primes.push_back(i);
cout << i << " " << endl;
}
// sum += i;
}
sum = accumulate(primes.begin(), primes.end(), 0);
cout << "Sum of the primes is: "<< sum;
// cout << "sum is: " << sum << endl;
return true;
}