#include #include #include /*Author: Carl Staab Last modified: October 28, 2000 Description: It's a prime number tester. What else? Writes to file primenum.txt I'm not sure what'll happen if you start/end on a prime- so just start/end on 1 or an even number. I just made the obvious improvement that if a num has any factors, we can break the loop and to start at 2 instead of (n^.5) - 1*/ void factortester(int start, int finish, ofstream a); void main() { int start, finish; ofstream a; a.open("primenum.txt"); cout << "Prime Number Tester\n\n"; cout << "Enter the (positive) number to start testing at "; cin >> start; cout << "Enter the (higher) number to finish testing at "; cin >> finish; factortester(start, finish, a); a.close(); return; } void factortester(int start, int finish, ofstream a) { int count, count_two, factor_exist = 0, num_prime = 0, flr; for (count = start; count <= finish; count++) { flr = floor(sqrt(count)); for (count_two = 1; (count_two <= flr && factor_exist < 2); count_two++) { if (count % count_two == 0) factor_exist++; } if (factor_exist <= 1) { a << count << endl; num_prime++; } factor_exist = 0; } cout << "There were " << num_prime << " prime numbers."; return; }