Advertisement
Guest User

Untitled

a guest
Dec 11th, 2020
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <time.h>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. bool isPrime(int num)
  9. {
  10.     if (num == 2) {
  11.         return true;
  12.     }
  13.     if (num <= 1 || num % 2 == 0) {
  14.         return false;
  15.     }
  16.  
  17.     double sqrt_num = sqrt(double(num));
  18.     for (int div = 3; div <= sqrt_num; div +=2)
  19.     {
  20.         if (num % div == 0) {
  21.             return false;
  22.         }
  23.     }
  24.     return true;
  25. }
  26.  
  27.  
  28. int main()
  29. {
  30.     int N = 10'000'000;
  31.     clock_t start, end;
  32.     std::vector<int> list(N);
  33.     list.clear();
  34.     start = clock();
  35.  
  36.     for (int i = 0; i < N; i++) {
  37.         bool prime = isPrime(i);
  38.         if (prime) {
  39.             list.push_back(i);
  40.         }
  41.     }
  42.  
  43.     end = clock();
  44.     cout << endl << endl << (end - start) / ((double) CLOCKS_PER_SEC);
  45.     cout << " sec \n";
  46.     return 0;
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement