Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- bool is_prime(unsigned int num);
- unsigned int find_bigger_prime(unsigned int num);
- std::vector<unsigned int> prime_lookup = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
- //Works only for 3 digit primes
- unsigned int find_bigger_prime(unsigned int num) {
- num += 2;
- while ((!is_prime(num)) && (num < 1000)) {
- num += 2;
- }
- return num;
- }
- bool is_prime(unsigned int num) {
- unsigned int num_sqrt = (unsigned int)sqrt(num);
- for (unsigned int i : prime_lookup) {
- if (i > num_sqrt) {
- break;
- }
- if ((num % i) == 0) {
- return false;
- }
- }
- return true;
- }
- int main(void) {
- unsigned int prime = 101;
- while (prime < 1000) {
- printf("%u ", prime);
- prime = find_bigger_prime(prime);
- }
- putchar('\n');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement