- #1
computerex
- 68
- 0
Hello. I wrote a simple program that helps me simplify radicals:
The problem happens when you try to break a perfect cube. The funny bit is that it works for some cases, and it doesn't work for others. For example, if you input 8 with an index of 3, the program works as expected, and says that it has found a perfect square. However if you enter 64 (4*4*4) it fails to recognize the perfect cube. I have ran this through the debugger, everything should work as expected...
Code:
#include <iostream>
#include <math.h>
typedef struct
{
unsigned int x, y;
}pair;
bool breakcube(unsigned int radi, unsigned short inx, pair& pr)
{
unsigned int cb = 0;
for (unsigned int i = 2; i < radi; i++)
{
cb=i;
for (unsigned short j = 1; j < inx; j++)
cb*=i;
for (unsigned int j = 1; j < radi; j++)
{
if (cb*j==radi)
{
pr.x = cb;
pr.y = j;
return true;
}
}
}
return false;
}
int main()
{
unsigned int radi = 0;
unsigned int cb = 0;
unsigned short inx = 0;
char buff[64];
while(true)
{
std::cout << "\nEnter radicand ('quit' to terminate): ";
std::cin >> buff;
if (!strcmp(buff, "quit"))
return 0;
radi=(unsigned int)atof(buff);
std::cout << "\nEnter index: ";
std::cin >> inx;
cb = (unsigned int)pow(radi, 1.0/inx);
if ((double)pow(radi, 1.0/inx) == cb) // force the comparison
{
std::cout << "\nPerfect " << inx << " : ";
for (unsigned short i = 0; i < inx; i++)
{
std::cout << cb;
if (i!=inx-1)
std::cout << " * ";
}
std::cout << " = " << pow(cb, inx);
continue;
}
pair pr;
if (breakcube(radi, inx, pr))
std::cout << "\n" << radi << " can be broken by: " << pr.x << " * " << pr.y;
else
std::cout << "\n" << radi << " cannot be broken";
}
return 0;
}
The problem happens when you try to break a perfect cube. The funny bit is that it works for some cases, and it doesn't work for others. For example, if you input 8 with an index of 3, the program works as expected, and says that it has found a perfect square. However if you enter 64 (4*4*4) it fails to recognize the perfect cube. I have ran this through the debugger, everything should work as expected...