I'm trying to create a function that receives a number as an argument and performs actions on that number to find out its closest powers of 2 that will then add up to that number. For example, if the user enters 4, the function will append 4 because it is already a power of 2. If the user enters a 14 the function should see that 14 is not a power of 2 and the closest powers of 2 that make up 14 are 2,4, and 8.
I am only going up to 2^9.
What i have so far:
def powers_finder(n): powers= i=0 total=0 while i<10: value=2**i total=total+value i=i+1 #This if statement is for if the user enters a power of 2 as n #Then the number will be appended right away into my powers list. if value==n: powers.append(value)
The problem here being if the user enters in lets say 5 as (n) 5 is made up of the power 2^2=4 and 2^0=1 4+1=5. How can i extend my function to include this process?