Application is attached.
This is a very cool (if you are just that little bit nerdy) application that generates prime numbers, utilizing the power of all the cores that you have on your computer (up to 10).
-Start Number: enter the number that you want to begin searching for primes at.
-End Number: enter the number that you want to stop searching for primes at.
-Number of cores: enter the number of cores on your computer for optimum performance. e.g. if you have a core 2 processor, enter 2. Note that you can have a number greater or less than the number of cores on your computer; processor usage will be shared between each thread but performance will be hampered.
At the end, the amount of time it took to complete the search in milliseconds (1000ms in 1second) is displayed. This could be used if you wanted to run a performance comparison between multiple computers.
The order in which numbers are displayed is roughly from lowest to highest. If you specify more than one core they will not be in exact numerical order.
CPP code:
#include <iostream>
#include "windows.h"
#include "time.h"
using namespace std;
int iUserEndNo;
int iCores;
HANDLE ScreenMutex;
bool bVarUsed;
DWORD dwThreadID[10];
HANDLE hThread[10];
// Function executed when program is terminated
void OnExit(void)
{
cout << "Bi Bi!";
}
// Search for prime Numbers
DWORD WINAPI FindPrime(LPVOID lpParameter)
{
int iEndNo = iUserEndNo;
int iStartNo = int(lpParameter);
double dRemainder;
bool bPrime;
// For continuous prime searching
if(iEndNo == 0){iEndNo = 2147483647;}
// Check numbers allocated to core
for(int iCurrentNo = iStartNo; iCurrentNo<=iEndNo; iCurrentNo+=iCores) // Increase by iCores so that each core does a different number
{
// Check for prime number
bPrime = true;
for(int n = 2;n<=iCurrentNo;n++)
{
dRemainder = iCurrentNo % n; // Determines remainder of calculation
// If a remainder is found then the No is not prime
if ( (dRemainder == 0) && (iCurrentNo != n) )
{
bPrime = false;
break;
}
}
// Tell user if prime number has been found
if (bPrime == true)
{
WaitForSingleObject(ScreenMutex,INFINITE); // Wait for screen output to become free
cout << "\n" << iCurrentNo;
ReleaseMutex(ScreenMutex); // Allow others to use screen output now that we are done
}
}
return(0);
}
void main(void)
{
atexit(OnExit);
// Find number to start searching at
cout << "Start number:";
int iUserStartNo;
cin >> iUserStartNo;
// Find number to stop searching at
cout << "End number (0 for continuous):";
cin >> iUserEndNo;
// Find number of cores on system
cout << "Number of cores: ";
cin >> iCores;
// Mutex's (manages screen output to prevent clogging)
ScreenMutex = CreateMutex(NULL,false,NULL);
// Save timer at start
clock_t StartTm = clock();
// Create threads to find primes (1 for each core)
for(int n = 0;n<iCores;n++)
{
int iThreadStart = iUserStartNo+n;
hThread[n] = CreateThread(0,0,FindPrime,(LPVOID) iThreadStart,0,&dwThreadID[n]);
}
// Wait for threads to complete
WaitForMultipleObjects((DWORD)iCores,hThread,TRUE,INFINITE);
clock_t EndTm = clock(); // Save timer at end
clock_t TakenTm = EndTm - StartTm; // Calculate time taken to search for primes
// Wait for entry before exiting and display time taken
cout <<"\n\n"<<"It took: "<<TakenTm<<"ms to finish.";
char Blank;
cin >> Blank;
return; // Exit
}
Screenshot: