Thanks to everyone who helped me out with my early version of the prime number calculator... Here is the complete and updated version including unsigned 64 bit integers...mmmm mm....... Lots of fun with the higher numbers, but my humble 1.86 gHz centrino with 1gb RAM slows to a crawl with numbers in excess of a billion or ten.... :-) Here is the complete program, if you want to have some fun with it:
Let me know what you think, or if you have any suggestions for improvements (other than graphics ;-P)
Code:
#include <iostream>
#include <math.h>
using namespace std;
unsigned __int64 knum;
unsigned __int64 i = 1;
unsigned __int64 pt = 0;
bool is_prime;
int main()
{
cout << endl << endl;
cout << "This Program calulates the prime numbers between" << endl;
cout << "a given start and end number.";
start: cout << endl << endl << "Please enter a starting number (enter 0 to exit): ";
cin >> i;
if (i == 0)
goto close;
cout << endl << "Please enter an ending number: ";
cin >> knum;
cout << endl << endl;
cin.get();
while ((i <= knum) && (i > 0))
{
is_prime = true;
for (pt = 2; pt <= sqrt(i); pt++) // all numbers are divided by 1 so start from 2
{
if (i % pt == 0)
is_prime = false;
}// only when the "for" cycle is done we know for sure if "i" is prime
if (is_prime)
cout << i << ", ";
i++;
}
goto start;
close: cin.get();
return 0;
}
Comment