Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
power of 2
#1
given an int argument, is it a power of two?

how efficiently can you code this function?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
http://www.exploringbinary.com/ten-ways-...-two-in-c/
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
>>> def is_power(num):
...     if type(num) == int:
...         if num & (num - 1) == 0:
...             return True
...         else:
...             return False

>>> is_power(8)
True

>>> is_power(3)
False

>>> is_power(16)
True

>>> is_power(2**529354)
True
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Just rewrite of wavric's solution:
def is_power(num):
    return isinstance(num, int) and num > 0 and num & (num - 1) == 0
Reply
#5
Likely nitpicking, but if you really want to compute this efficiently, it is because you are facing large numbers, so checking for int is rather limiting... Check for int or long, or better do not check and catch the exception...
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#6
the way i did it was pretty much like how wavic did it except i did not bother with the two if tests and just did it as return num&num-1==0 (variable names were changed to protect the innocent).

choosing the best algorithm is the first step of good programming (coding).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
As I know, the integer in Python can be as long as we need.

>>> type(2**(10**6))

<class 'int'>
@Ofnuts gave a useful link. I see that the algorithm I proposed is not the fastest one.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
Maybe Ofnuts means something about internal representation of int? There used to be "short" ints (32 or 64bit ones) and long ints in python2, but it was unified in python3 (and long() was removed).

Python 2.7
Output:
>>> x, y = 1, 1L >>> type(x), type(y) (<type 'int'>, <type 'long'>)
I am not sure if performance comparison in Ofnuts' link applies to python - fastest algorithm uses binary complement and that depends on fixed bit length.  return num & num - 1 == 0 seems to perform well and its easy to understand.
Reply
#9
(Mar-14-2017, 10:05 AM)wavic Wrote: As I know, the integer in Python can be as long as we need.

>>> type(2**(10**6))

<class 'int'>

Not in Python V2
Output:
Python 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> type(2**(10**6)) <type 'long'>
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  EVGA Power Suppiles on Sale Larz60+ 0 1,948 Oct-30-2018, 02:19 PM
Last Post: Larz60+
  Power phase question. Sleeper 4 3,788 Sep-03-2018, 10:00 AM
Last Post: DeaD_EyE
  Snow storm over - power back on Larz60+ 4 3,198 Mar-12-2018, 12:15 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020