• What is your GameDev Story?

Archived

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

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

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

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 on other sites
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 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 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 on other sites
It turns out what I was looking for was not power but rather multiples. I write the script and now it''s workin, thanks

MindEngine Development
http://medev.sourceforge.net

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 on other sites
No offense guys, but that was just a simple division problem.

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 on other sites
To address the original (but not meant) question: One would use the good ole log-function for things like finding the power.

Share on other sites

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

This topic is now closed to further replies.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• Forum Statistics

• Total Topics
634044
• Total Posts
3015208
×