1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
string anstolower(string s)
{
string result = "";
for(int i = 0; i < s.length(); i++)
{
result += tolower(s[i]);
}
return result;
}
int main()
{
// ========!===============!===============!===============!===============!========
// VARIABLES
// ========!===============!===============!===============!===============!========
unsigned int num, test; // Number user chooses to test, value testing primality.
unsigned int factor, outcome; // Number of factors found, value to determine if a factor.
unsigned int halfNum; // Half of the input number.
int primeNum=1; // Number of prime factorizations performed.
string input; // User input for Yes/No question.
string low; // tolower'd form of input.
// ========!===============!===============!===============!===============!========
// GREETINGS
// ========!===============!===============!===============!===============!========
// Welcome the user.
cout << "\n\n ===============!===============!===============!==============="
<< "\n ~!~ Welcome to the Prime Factorizationing ~!~"
<< "\n ===============!===============!===============!==============="
<< "\n\n This program will calculate the primality of a number, and if it is"
<< "\n not prime, the factors and number of factors of the input number.";
// START OF PROGRAM END CHECK LOOP
do {
// START OF PROGRAM LOOP.
do {
// Reset testing variables to allow computation for new numbers.
test=2; // Sets the starting divisor to 2 to begin testing for factors.
factor=0; // Sets the number of factors of the input number to zero.
// Numbers each factorization for easier following.
cout << "\n\n\n Prime Number Test # " << setw(2) << primeNum << "";
// Increases the number for record of later factorizations.
primeNum ++;
// ========!===============!===============!===============!===============!========
// USER INPUT
// ========!===============!===============!===============!===============!========
// Prompt user for number to be tested.
cout << "\n\n Please input a number to calculate if it is prime. : ";
cin >> num;
// ========!===============!===============!===============!===============!========
// CALCULATION
// ========!===============!===============!===============!===============!========
// START OF FACTOR FINDING LOOP.
do {
halfNum = num/2;
// If outcome does not equal zero, test was a factor of the number.
outcome = num%test;
// If outcome does not equal zero, shows the user the found factor.
if (outcome == 0) {
// PROBLEM WITH CALCULATION OF SQUARE ROOTS.
// If test is equal to half of the input number and it is a factor, it is the square
// root of the number, so only 1 factor was found.
if (test == halfNum) {
// The test number was the factor found, so show test.
cout << "\n " << setw(10) << test << " is a factor of " << num << ".";
// There was only 1 factor, so factor increases by 1.
factor++;
}
// If the found factor was not half of the input number, then two factors were found.
else {
// Test number and its sister factor, found by (num/test), are displayed.
cout << "\n " << setw(10) << test << " and " << (num/test) << " are factors of "
<< num << ".";
// There were 2 factors found, so factor increases by 2.
factor += 2;
}
}
// Increases the test variable by 1 for the loop to continue.
test++;
/* (test <= (num/test)) keeps test values smaller than otherwise since the numbers over num/2
were previously calculated. */
// END OF FACTOR FINDING LOOP
} while (test <= (num/test));
// ========!===============!===============!===============!===============!========
// FINAL OUTCOME
// ========!===============!===============!===============!===============!========
// Inform user of the outcome.
cout << "\n\n ===============!===============!===============!===============";
// SINCE THERE IS AN UNRESOLVED PROBLEM WITH SQUARE ROOTS UPSTREAM, THIS SECTION IS FLAWED.
// If there was only 1 factor, display that factor (test was increased by 1 before the loop
// terminated, so to find the factor it is (test1).
if (factor == 1) {
cout << "\n The only factor of " << num << " is " << (test1) << " (excluding"
<< " 1 and " << num << ").";
}
// If there was more than one factor, display the number of factors found.
else if (factor != 0) {
cout << "\n There are " << factor << " factors of " << num << " (excluding"
<< " 1 and " << num << ").";
}
// If there were no factors, input number is prime.
else {
cout << "\n The input number is prime." << endl;
}
cout << "\n ===============!===============!===============!===============";
// Prompt for calculation of primality of a new number or to terminate program.
cout << "\n\n Would you like to calculate the primality of a new number? Yes or No? : ";
cin.ignore();
getline(cin,input);
// Validate input.
low = anstolower(input);
while((low != "y") && (low != "yes") && (low != "n") && (low != "no")) {
cout << " Invalid choice. Please choose Yes or No. : ";
getline(cin,input);
low = anstolower(input);
}
// END OF PROGRAM LOOP
} while ((low == "yes")  (low == "y"));
// Check to see if the user wanted to terminate program.
cout << "\n Are you sure? Type Yes to close program, No to calculate the primality of a"
<< "\n\tnew number. : ";
getline(cin,input);
// Validate input.
low = anstolower(input);
while((low != "y") && (low != "yes") && (low != "n") && (low != "no")) {
cout << " Invalid choice. Please choose Yes or No. : ";
getline(cin,input);
low = anstolower(input);
}
// END OF PROGRAM END CHECK LOOP
} while ((low == "no")  (low == "n"));
return 0;
}
