Advertisement
karbaev

primes-eratosphen

Feb 29th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     const int max = 1000000;
  7.     int count = 1;
  8.     bool arr[max];
  9.  
  10.     for(int i = 0; i < max; ++i)
  11.         arr[i] = true;
  12.  
  13.     for(int i = 2; i < max-1; ) {
  14.         if(count == 10001){ //вывод 10001 простого числа
  15.             cout<<i<<endl;
  16.             break;
  17.         }
  18.         //помечаем все кратные
  19.         for(int j = 2; (j*i) < max-1; ++j){
  20.             arr[i*j] = false;
  21.         }
  22.         //находим следующее простое
  23.         for(int k = i+1; k < max-1; k++){
  24.             if(arr[k]) {
  25.                 i=k;
  26.                 ++count;
  27.                 break;
  28.             }
  29.         }
  30.     }
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement