#include <iostream>
#include <windows.h>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <string.h>
double numerator, denominator;
static short multiplycounter;
static short dividecounter;
const double ten = 10;
double* ppnumerator = &numerator;
double* ppdenominator = &denominator;
using namespace std;
/**********************************************************************************/
double lcm(double numerator, double denominator)
{
if(*ppnumerator > *ppdenominator)
{
for(long counter = long(*ppdenominator) / 2; counter > 1; counter--)
{
if(long(*ppdenominator) % counter == 0 && long(*ppnumerator) % counter == 0)
{ *ppdenominator /= counter, *ppnumerator /= counter; }}}
//********************************************************************
if(*ppnumerator < *ppdenominator)
{
for(long counter = long(*ppnumerator) / 2; counter > 1; counter--)
{
if(long(*ppdenominator) % counter == 0 && long(*ppnumerator) % counter == 0)
{ *ppdenominator /= counter, *ppnumerator /= counter; }}}
}
bool isprime(long denominator)
{
long max = long(sqrt(denominator));
if(denominator <= 1)
{ return true; }
if(denominator % 2 == 0)
{ return false; }
for( long counter = 2; counter <= max; counter++)
if(denominator % counter == 0)
{ return false; }
}
bool isprime(double x)
{
long max = long(x / 2); /* Cast it to remove warnings*/
if (long(x) <= 0 || long(x) == 1 || (long(x) % 2 == 0 && long(x) != 2))
{ return false; }
for (long counter = 3; counter < max; counter++)
if (long(x) % counter == 0)
{ return false; }
return true;
}
int main()
{
static double decimal, ndecimal;
cin >> decimal;
ndecimal = floor(decimal);
if(decimal != ndecimal)
{
for(multiplycounter = 0; decimal != floor(decimal); multiplycounter++)
{
if(decimal != floor(decimal))
decimal *= 10;
}//loop
cout << "\nNew non-decimal number: " << decimal;
cout << "\nNumber of times multiplied by ten: " << multiplycounter << "\n\n";
}//if statement
else
{ cout << "That number isn't a decimal!"; }
numerator = long(decimal);
denominator = pow(ten, static_cast<int>(multiplycounter));
cout << numerator << " / " << denominator << "\n\n";
if(isprime(numerator) || isprime(denominator))
{ cout << "Cannot be reduced!\n"; }
while(!isprime(denominator))
{ lcm(numerator, denominator);
if(isprime(denominator) || isprime(numerator))
{ cout << *ppnumerator << " / " << *ppdenominator; break;}
}
/**********************************************************************************/
cin.clear();
cin.ignore(255, '.');
cin.get();
return 0;
}
Most of this program works. Depending on the decimal number you feed it, it will work properly. But some numbers (515.55) won't. Why? I found that with that number, (and others) the program gets stuck looping the isprime** function. Again, why?
**I know I have two isprimes, because when I found that the program was getting stuck there I looked one up on the internet to see if it was a problem in my code. I have the same problem with either function, though. The first one is mine, the second one is one I found.