Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is n = a^b?

Status
Not open for further replies.

bbone

Programmer
Aug 18, 2003
10
SI
Hi all!

I need a function to check if there exist numbers a and b so that n = a^b. I used this function and I thought it works correct, but it doesn't. Does anyone has any better solution?
Thanks

if (n == 1 || n == 2 || n == 3)
return 0;

unsigned long faktor = ceil(sqrt(n)) + 1 ;
for(unsigned long integral=2; integral<=faktor; integral++) {
double result=log10(n)/log10(integral);
if(fmod(result,1)==0) {
return 1;
}
}
return 0;
 
The first problem to overcome is that all floats are approximations.
What appears to be a mathematical solution does not mean that it is a computable solution on a finite machine. You have to contend with range and precision problems, and these introduce all sorts of rounding errors into your calculations.

Are you working on factorisation?

If you are, normal machine types (like int and float) are useless for the size of numbers factorisation typically deals with. You really need one of the 'bignum' packages which are available
Here's one example

--
 
when you're using float/double numbers you can not rely on operator ==. YOu can use only >, <, >=, <=. To check if two float/double numbers are equal use >=, <=.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top