Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- int n;
- cin >> n;
- bool cut[n + 1]; // list of number from 1 to n that has been discarded (ignore 0 and 1)
- for (int i = 0; i <= n; i++) // initialize false
- cut[i] = false;
- // keep cutting
- int curMin = 2;
- while (curMin <= n)
- {
- // starts cutting from second multiple of it
- // for example, curMin = 2 will cut 4, 6, 8, 10, ...
- for (int i = 2 * curMin; i <= n; i += curMin)
- cut[i] = true;
- // finds next not-yet-cut number (prime number)
- do
- {
- curMin++;
- } while (cut[curMin]);
- }
- // print all non-cut number
- for (int i = 2; i <= n; i++)
- {
- if (!cut[i])
- cout << i << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement