#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
void Eratos(int &length, vector< int > &);
ostream &operator<< (ostream &, const vector <int>&);
bool isPrime(int);
int main()
{
int range;
vector< int > v;
cout << "Enter the upper range of number to search for prime numbers\n";
cin >> range;
if (range > 300 || range < 2)
{
cout << "Invalid range. Your range is not between 2 and 300\n";
}
else
{
Eratos(range, v);
}
return 0;
}
void Eratos(int &length, vector< int > &v)
{
for (int i = 2; i < length; ++i)
{
v.push_back(i);
}
for (int i = 2; i < v.size(); ++i)
{
if (isPrime(i) == false)
{
v.erase(v.begin() + i);
}
}
cout << "Prime numbers are:\n";
cout << v;
}
ostream &operator <<(ostream& print, const vector<int> &v)
{
int count = 1;
for (unsigned int i = 0; i < v.size(); ++i)
{
print << setw(5) << v[i];
if (count % 10 == 0)
{
cout << '\n';
}
count++;
}
print << '\n';
return print;
}
bool isPrime(int i)
{
for (int j = 2; j < i; ++j)
{
if (i % j == 0)
{
return false;
}
}
return true;
}
The problem is my code takes away some prime but leaves other primes in and i cant seem to figure out why
like my output is
2 3 4 5 7 8 10 11 13 15
17 18 20 21 23 25 27 28 30 31
33 35 37 38 40 42 44 46 48 49
51 52 54 56 58 60 62 63 65 67
69 70 72 73 75 77 79 80 82 84
86 88 90 91 93 95 97 99
But the output should be
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97

New Topic/Question
Reply


MultiQuote





|