Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

neurokaotix

How to determine if one number is a power of another?

This topic is 5195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can''t remember how to do this for the life of me... How to determine if one number is a power of another? It''s for a shopping cart script. Let''s say some barstools need to be ordered in pairs of 2, so I have the script check and see if the quantity entered is a power of 2, right? MindEngine Development http://medev.sourceforge.net

Share this post


Link to post
Share on other sites
Advertisement
In that example:

if (NumStools % PackageSize == 0)
cout<<"That is a valid order.";
else
cout<<"Invalid order, you can offly order in sets of " << PackageSize << ".";

If by power of 2 you mean: 2, 4, 8, 16, 32, etc. (Every programmers favorite number sequence. )

Mmmm...

There is most likely a better way, but just winging it I would...


int IsPower(double a, double b)
{
while (a > b)
{
a = a / b;
i++;
}

if (a == b)
return i + 1;
else
return 0;
}


Where a is the number in question, and b is the number you want to see if a is a power of.

Example if A was 16, and B was 2.
It would return 4. Meaning that b^i=a. 2^4=16
If it returns 0 it's not a power.

[edited by - Cyber-Ace on March 5, 2004 2:39:46 PM]

[edited by - Cyber-Ace on March 5, 2004 2:40:37 PM]

[edited by - Cyber-Ace on March 5, 2004 2:41:31 PM]

Share this post


Link to post
Share on other sites
What you are looking for is not a power of 2 (2n) but instead a multiple, for which modula (%) is used as shown above.

[edited by - cozman on March 5, 2004 2:34:25 PM]

Share this post


Link to post
Share on other sites
The powers of 2 are: 1, 2, 4, 8, 16, 32, 64, 128, ...

That''s probably not what you are after. You probably meant multiples of 2, which are: 0, 2, 4, 6, 8, 10, ...

To check if a number x is a multiple of another number y, check if x/y is an integer (Cyber-Ace''s code does that).

To check if a number x is a power of another number y, check if log(x)/log(y) is an integer.

Share this post


Link to post
Share on other sites
... you mean I updated my hasty post for nothing?



Well, it was fun thinking through a little problem like that.

Share this post


Link to post
Share on other sites
quote:
Original post by slackey
No offense guys, but that was just a simple division problem.


Yes, becuause dividing a number by two will return TRUE if it''s divisible by two, and FALSE if not. Oh wait, it doesn''t, so it''s not.

<- Cow Soft, free software I''ve made
"Gay marriage will encourage people to be gay, in the same way that hanging around tall people will make you tall." - Grizwald

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
To address the original (but not meant) question: One would use the good ole log-function for things like finding the power.

Share this post


Link to post
Share on other sites

This topic is 5195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!