Skip to content

Instantly share code, notes, and snippets.

@tomeaton17
Created August 6, 2018 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomeaton17/d94c5a2963116ea84129b1e83ffcebb6 to your computer and use it in GitHub Desktop.
Save tomeaton17/d94c5a2963116ea84129b1e83ffcebb6 to your computer and use it in GitHub Desktop.
to size_t or not to size_t?
bool is_prime(int number, vector<int> primes)
{
bool is_prime {true};
size_t counter {0};
while (is_prime && counter < primes.size()) {
if (number % primes[counter] == 0) {
is_prime = false;
}
++counter;
}
return is_prime;
}
int main() {
vector<int> primes;
primes.push_back(2); // Have to give first prime for is_prime algorithm to work
size_t max {0};
cout << "Enter the upper bound of the prime number search: ";
cin >> max;
for (size_t i = 0; i <= max; ++i) {
if (is_prime(i, primes))
primes.push_back(i);
}
for (int x : primes)
cout << x << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment