This is the program. It makes much use (or rather abuses heavily) casting to properly deal with the numbers it has to work with. The only Achilles' heel to the entire program is the limitation of numerical precision. It tends to fall apart (or just plain takes too long for my taste) large numbers. This however is only a limitation of the computer's hardware and not of the program itself.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
char check = 'y';
while (check == 'y') {
int P = 2; int D = 0; int M = 0; int R = 0; int N = 0;
cout << "Enter a number:";
cin >> N;
cout << endl;
R = N;
while ((P < (N + 1)) && (R >= P)) {
M = 0;
if (((R % int(pow(double(P), double(M + 1)))) == 0) && ((R / int(pow(double(P), double(M + 1)))) >= 1)) {
while (((R % int(pow(double(P), double(M + 1)))) == 0) && ((R / int(pow(double(P), double(M + 1)))) >= 1)) {
M++ ; D++;
}
R = (R / int(pow(double(P), double(M))));
cout << P << " is a factor of " << N << " with a multiplicity of " << M << ". \n";
}
P++;
}
if (D == 1) {
cout << "Which means " << N << " is a prime number. \n" ;
}
cout << "Again (y/n)?" ;
cin >> check ;
cout << endl;
}
return 0;
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.