Skip to content

Instantly share code, notes, and snippets.

@manvillej
Created November 17, 2018 15:28
Show Gist options
  • Save manvillej/3b13a82f1e7420e490e41041bd50c619 to your computer and use it in GitHub Desktop.
Save manvillej/3b13a82f1e7420e490e41041bd50c619 to your computer and use it in GitHub Desktop.
breaks an integer into an array of numbers that represent the number as a series of 2 to a power in order from lowest to highest.
def getArrayOfMultiplesOfTwos(number):
"""
breaks an integer into an array of numbers that represent the number as a series of 2 to a power in order from lowest to highest.
for example: 9 = 2**0 + 2**3.
>>> getArrayOfMultiplesOfTwos(9)
[0, 3]
"""
array = list()
while number != 0:
baseTwo = int(math.log(number,2))
array.append(baseTwo)
number = number - 2**baseTwo
array.reverse()
return array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment