Basically, I need a function that will divide n by two and return the number of times it can be done.
Coding so far:
def div(n):
while n >= 0:
n / 2
return n
I know for a fact that I have to use the while loop, but I'm not confident in my third line of coding. What am I doing wrong?
Examples:
>>> div(4)
2
>>> div(7)
2
Any number can be divided by two an infinite number of times. 32 / 2 = 16.
Divide - traditional method (long division) The division of two numbers is the process of calculating the number of times one number is contained in another.
Therefore, a number is divisible by 2 if it has a 0, 2, 4, 6, or 8 in the ones place. For example, 54 and 2,870 are divisible by 2, but 2,221 is not divisible by 2.
A number is divisible by 5 if the number's last digit is either 0 or 5. Divisibility by 5 - examples: The numbers 105, 275, 315, 420, 945, 760 can be divided by 5 evenly.
An integer n can be divided by 2: floor(log(n)/log(2))
times.
/ does not perform assignment. Since you're returning n and not changing its value, I'd think you should start there. Your other hints are = and %.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With