Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <fstream>
- #include <ctime>
- #include <Windows.h> //Sleep
- using namespace std;
- /*
- (Number Limits)
- int : 2147483647
- uint : 4294967295
- */
- #define MAX_NUM 10000000 // 2.1kkk for int, 4.2kkk for uint
- // Our file where we will store prime numbers
- ofstream myFile;
- // Function Declaration
- bool isPrime(unsigned int N); // Return if a number is Prime number
- bool OpenFile(); // Opens the file where we will store prime numbers
- bool CloseFile(); // Close the file
- void Write(unsigned int n); // Write to file
- int main()
- {
- clock_t startTime = clock();
- printf("This process will take a lot of time\n\n");
- // Small delay before start
- while((clock() - startTime) < 2000)
- { }
- system("cls");
- // Prepeare file for write
- if (!OpenFile())
- printf("File failed to (open)!");
- clock_t temp = startTime;
- int tempDelay = 1000; //1 sec
- #pragma omp parallel num_threads(2)
- {
- #pragma omp for
- for(unsigned int i=0; i < MAX_NUM; i++)
- {
- if (isPrime(i))
- {
- Write(i);
- if ((clock() - temp) >= tempDelay)
- {
- system("cls");
- printf("[%d%%] %d / %d till now has elapsed (%d) seconds\n", i*100 / MAX_NUM, i, MAX_NUM, abs(clock()/1000));
- temp += tempDelay;
- }
- }
- }
- }
- clock_t endTime = clock();
- clock_t duration = ( clock() - startTime ) / (double) CLOCKS_PER_SEC;
- printf("\nStart Time:\t%d milliseconds\n", startTime);
- printf("End Time :\t%d milliseconds\n", endTime);
- printf("Duration :\t%d minutes\n", duration/60);
- printf("Duration :\t%d seconds\n", duration);
- // Write time info to file
- Write(startTime);
- Write(endTime);
- Write(duration);
- // Close file, our work is finished
- if (!CloseFile())
- printf("File failed to (close)!");
- return 0;
- }
- // Return if a number is Prime number
- bool isPrime(unsigned N)
- {
- // 2 is prime number
- if (N == 2)
- return true;
- // If rest of an division is 0, then is not prime number
- if (N % 2 == 0)
- return false;
- if (N >= 2)
- {
- #pragma omp parallel num_threads(2)
- {
- #pragma omp for
- for(unsigned int i=3; i <= sqrt((float)N); i+=2)
- {
- if ((N % i) == 0)
- return false;
- }
- }
- }
- else
- return false;
- return true;
- }
- // Opens the file where we will store prime numbers
- bool OpenFile()
- {
- try {
- myFile.open("prime2.txt");
- }
- catch(...){
- return false;
- }
- return true;
- }
- // Close the file
- bool CloseFile()
- {
- try {
- myFile.flush();
- myFile.close();
- }
- catch(...){
- return false;
- }
- return true;
- }
- // Write to file
- void Write(unsigned int n)
- {
- myFile << n << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement