Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <Windows.h>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     DWORD tc=GetTickCount();
  10.     int prim, n, i, j=2;
  11.     unsigned long long sum=5;
  12.     int primek[150000];
  13.     primek[0]=2;
  14.     primek[1]=3;
  15.     for(int n=5; n<2000000; n+=2)
  16.     {
  17.         prim=1;
  18.         int n_sqrt=sqrt(n);
  19.         for(int i=1; primek[i]<=n_sqrt; ++i)
  20.         {
  21.             if(n%primek[i]==0)
  22.             {
  23.                 prim=0;
  24.                 break;
  25.             }
  26.         }
  27.         if(prim)
  28.         {
  29.             sum+=n;
  30.             primek[j++]=n;
  31.         }
  32.     }
  33.  
  34.     cout << "All primes:\t" << j << endl;
  35.     cout << "Sum:\t\t" << sum << endl;
  36.     cout << "Time elapsed:\t" << (int)((GetTickCount()-tc)/1000)
  37.     <<'.'<<((GetTickCount()-tc)-((GetTickCount()-tc)/1000)*1000);
  38.     cout << endl;
  39.     system("pause");
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement