Advertisement
Guest User

cprime

a guest
May 24th, 2013
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <fstream>
  5. #include <ctime>
  6. #include <Windows.h>    //Sleep
  7.  
  8. using namespace std;
  9.  
  10. /*
  11.     (Number Limits)
  12.  
  13.     int : 2147483647
  14.     uint    : 4294967295   
  15. */
  16.  
  17. #define MAX_NUM 10000000    // 2.1kkk for int, 4.2kkk for uint
  18.  
  19. // Our file where we will store prime numbers
  20. ofstream myFile;
  21.  
  22. // Function Declaration
  23. bool isPrime(unsigned int N);   // Return if a number is Prime number
  24. bool OpenFile();        // Opens the file where we will store prime numbers
  25. bool CloseFile();       // Close the file
  26. void Write(unsigned int n); // Write to file
  27.  
  28. int main()
  29. {
  30.     clock_t startTime = clock();
  31.     printf("This process will take a lot of time\n\n");
  32.  
  33.     // Small delay before start
  34.     while((clock() - startTime) < 2000)
  35.     { }
  36.     system("cls");
  37.  
  38.     // Prepeare file for write
  39.     if (!OpenFile())
  40.         printf("File failed to (open)!");
  41.  
  42.     clock_t temp = startTime;
  43.     int tempDelay = 1000;   //1 sec
  44.    
  45.     #pragma omp parallel num_threads(2)
  46.     {
  47.         #pragma omp for
  48.         for(unsigned int i=0; i < MAX_NUM; i++)
  49.         {
  50.             if (isPrime(i))
  51.             {
  52.                 Write(i);
  53.  
  54.                 if ((clock() - temp) >= tempDelay)
  55.                 {
  56.                     system("cls");
  57.                     printf("[%d%%] %d / %d till now has elapsed (%d) seconds\n", i*100 / MAX_NUM, i, MAX_NUM, abs(clock()/1000));
  58.                     temp += tempDelay;
  59.                 }
  60.             }
  61.         }
  62.     }
  63.  
  64.     clock_t endTime = clock();
  65.     clock_t duration = ( clock() - startTime ) / (double) CLOCKS_PER_SEC;
  66.     printf("\nStart Time:\t%d milliseconds\n", startTime);
  67.     printf("End Time  :\t%d milliseconds\n", endTime);
  68.     printf("Duration  :\t%d minutes\n", duration/60);
  69.     printf("Duration  :\t%d seconds\n", duration);
  70.  
  71.     // Write time info to file
  72.     Write(startTime);
  73.     Write(endTime);
  74.     Write(duration);
  75.  
  76.     // Close file, our work is finished
  77.     if (!CloseFile())
  78.         printf("File failed to (close)!");
  79.    
  80.  
  81.     return 0;
  82. }
  83.  
  84. // Return if a number is Prime number
  85. bool isPrime(unsigned N)
  86. {
  87.     // 2 is prime number
  88.     if (N == 2)
  89.         return true;
  90.  
  91.     // If rest of an division is 0, then is not prime number
  92.     if (N % 2 == 0)
  93.         return false;
  94.  
  95.     if (N >= 2)
  96.     {
  97.         #pragma omp parallel num_threads(2)
  98.         {
  99.             #pragma omp for
  100.             for(unsigned int i=3; i <= sqrt((float)N); i+=2)
  101.             {
  102.                 if ((N % i) == 0)
  103.                     return false;
  104.             }
  105.         }
  106.     }
  107.     else
  108.         return false;
  109.  
  110.     return true;
  111. }
  112.  
  113. // Opens the file where we will store prime numbers
  114. bool OpenFile()
  115. {
  116.     try {
  117.         myFile.open("prime2.txt");
  118.     }
  119.     catch(...){
  120.         return false;
  121.     }
  122.  
  123.     return true;
  124.  
  125. }
  126.  
  127. // Close the file
  128. bool CloseFile()
  129. {
  130.     try  {
  131.         myFile.flush();
  132.         myFile.close();
  133.     }
  134.     catch(...){
  135.         return false;
  136.     }
  137.  
  138.     return true;
  139.  
  140. }
  141.  
  142. // Write to file
  143. void Write(unsigned int n)
  144. {
  145.     myFile << n << endl;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement