Advertisement
Guest User

Untitled

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