Created
August 6, 2018 22:30
-
-
Save tomeaton17/d94c5a2963116ea84129b1e83ffcebb6 to your computer and use it in GitHub Desktop.
to size_t or not to size_t?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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