Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <stdlib.h>
- using namespace std;
- void listPrimes(int x);
- int main() {
- listPrimes(5000);
- }
- void listPrimes(int x) {
- bool *not_prime = new bool[x];
- unsigned j = 0, i = 0;
- for (i = 0; i <= x; i++) {
- if (i < 2) {
- not_prime[i] = true;
- } else if (i % 2 == 0 && i != 2) {
- not_prime[i] = true;
- }
- }
- while (j <= x) {
- for (i = j; i <= x; i++) {
- if (!not_prime[i]) {
- j = i;
- break;
- }
- }
- for (i = (j * 2); i <= x; i += j) {
- not_prime[i] = true;
- }
- j++;
- }
- for ( i = 0; i <= x; i++) {
- if (!not_prime[i])
- cout << i << ' ';
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement