Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- vector<int> sieve_of_eratosthenes(int n) {
- vector<bool> prime(n + 1, true);
- prime[0] = prime[1] = false;
- for (int i = 2; i <= n; ++i)
- if (prime[i])
- if (i * 1ll * i <= n)
- for (int j = i * i; j <= n; j += i)
- prime[j] = false;
- vector<int> a;
- for (int i = n; i >= 2; i--) {
- if (a.size() == 10)
- break;
- if (prime[i])
- a.push_back(i);
- }
- reverse(a.begin(), a.end());
- return a;
- }
- void test() {
- vector<int> a = sieve_of_eratosthenes(100);
- int chek[] = { 53,59,61,67,71,73,79,83,89,97 };
- bool id = false;
- for (int i = 0; i < 10; i++) {
- if (chek[i] != a[i])
- id = true;
- }
- if (!id) {
- cout << "Test 1:OK" << endl;
- }
- else
- {
- cout << "Test 1:Error" << endl;
- id = false;
- }
- a = sieve_of_eratosthenes(200);
- int chek2[] = { 157,163,167,173,179,181,191,193,197,199 };
- for (int i = 0; i < 10; i++) {
- if (chek2[i] != a[i])
- id = true;
- }
- if (!id) {
- cout << "Test 2:OK" << endl;
- }
- else
- {
- cout << "Test 2:Error" << endl;
- id = false;
- }
- }
- int main()
- {
- int n;
- cout << "Enter the number" << endl;
- while (!scanf_s("%d", &n) || n < 0) {
- rewind(stdin);
- cout << "Enter the correct sentence" << endl;
- }
- vector<int> a = sieve_of_eratosthenes(n);
- for (int i = 0; i < a.size(); i++) {
- cout << a[i] << " ";
- }
- cout << endl;
- test();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement