src/primeNumbersCalculatorSieve.cpp
// This counter stores the number of primes we have calculated in the process

This counter stores the number of primes we have calculated in the process
This counter stores the number of primes we have calculated in the process
int primeNumbersCalculatorSieve() {
	setlocale(LC_NUMERIC, "el_GR");
	clrscr();
	std::cout << "Prime Numbers Calculator: Sieve of Eratosthenes (To " << 100000 << ")" << std::endl;
	std::cout << "Press any key to continue . . ." << std::endl;
	system("pause>nul");
	std::cout << std::endl;
	for (int arrayIterator = 3; arrayIterator <= numbersToTest; ++arrayIterator) { 
		arrayOfPrimes[arrayIterator] = arrayIterator % 2; 
	}
	primeMultiplier = 0; 
	QueryPerformanceFrequency(&qFrequency); 
	QueryPerformanceCounter(&qCount1); 
	for (short iterator1 = 3; iterator1 < sqrtPrime; iterator1 += 2) { 
		if (arrayOfPrimes[iterator1]) { 
			for (int iterator2 = 3; iterator2 < (numbersToTest/3); iterator2 += 2) { 
				primeMultiplier = iterator1 * iterator2; 
				if (primeMultiplier > numbersToTest) { 
					break; 
				}
				arrayOfPrimes[primeMultiplier] = false; 
			}
		}
	}
	QueryPerformanceCounter(&qCount2); 
	counter = 1;
	for (int arrayCountUp = 0; arrayCountUp <= numbersToTest; ++arrayCountUp) {
		if (arrayOfPrimes[arrayCountUp] == true) { 
			counter++;
		}
	}
	totaltime = (long float)(((long float)qCount2.QuadPart - (long float)qCount1.QuadPart) * 100000 / qFrequency.QuadPart) / 100000; 
	std::cout << "Total time (sec): " << totaltime << std::endl; 
	std::cout << counter << " primes found." << std::endl; 
	std::cout << "Press any key to continue . . ." << std::endl; 
	system("pause>nul");
	primesMenu();
	return 0;
}
int primeNumbersCalculatorSieve ( ) { setlocale ( LC_NUMERIC , " " ) ; clrscr ( ) ; std :: cout << " " << 100000 << " " << std :: endl ; std :: cout << " " << std :: endl ; system ( " " ) ; std :: cout << std :: endl ; for ( int arrayIterator = 3 ; arrayIterator <= numbersToTest ; ++ arrayIterator ) { arrayOfPrimes [ arrayIterator ] = arrayIterator % 2 ; } primeMultiplier = 0 ; QueryPerformanceFrequency ( & qFrequency ) ; QueryPerformanceCounter ( & qCount1 ) ; for ( short iterator1 = 3 ; iterator1 < sqrtPrime ; iterator1 += 2 ) { if ( arrayOfPrimes [ iterator1 ] ) { for ( int iterator2 = 3 ; iterator2 < ( numbersToTest / 3 ) ; iterator2 += 2 ) { primeMultiplier = iterator1 * iterator2 ; if ( primeMultiplier > numbersToTest ) { break ; } arrayOfPrimes [ primeMultiplier ] = false ; } } } QueryPerformanceCounter ( & qCount2 ) ; counter = 1 ; for ( int arrayCountUp = 0 ; arrayCountUp <= numbersToTest ; ++ arrayCountUp ) { if ( arrayOfPrimes [ arrayCountUp ] == true ) { counter ++ ; } } totaltime = ( long float ) ( ( ( long float ) qCount2 . QuadPart - ( long float ) qCount1 . QuadPart ) * 100000 / qFrequency . QuadPart ) / 100000 ; std :: cout << " " << totaltime << std :: endl ; std :: cout << counter << " " << std :: endl ; std :: cout << " " << std :: endl ; system ( " " ) ; primesMenu ( ) ; return 0 ; }
This counter stores the number of primes we have calculated in the process
This counter stores the number of primes we have calculated in the process
// set the locale for the program
 // This loop simply iterates up to the number we want to end at
 // This will fill the array of primes with bools, half odd and half even
 // To start with, reset the prime multiplier (explained below) incase the program is being rerun
 // This basically detects how good the current version of windows is at keeping time
 // Begin the timer
 // Firstly, we run through and work out all the square roots
 // Basically, this if statement saves us time by not having to look at the same numbers twice (checks if iterator1 is true basically)
 // This loop basically goes through and multiplies the first iterator with the second
 // Here is where the multiplication happens
 // This basically checks to see if the multiplied value is higher than the maximum value
 // ... and if it is, break the for loop
 // If the for loop is not broken above, this line will make the number not a prime in the bool array
 // Kill the timer
 // This basically goes through and counts up all the true values in the array (aka the primes)
 // Make totaltime the difference between qCount2 and qCount1
 // Print totaltime
 // ... and the number of primes found...
 // This asks the user to end the program
