on writing a number as 2^s * q, where q is odd

jak nospam at please.ty
Sun Dec 3 01:26:11 EST 2023


Julieta Shem ha scritto:
> jak <nospam at please.ty> writes:
> 
> [...]
> 
>>> --8<---------------cut here---------------start------------->8---
>>> def powers_of_2_in(n):
>>>     if remainder(n, 2) != 0:
>>>       return 0, n
>>>     else:
>>>       s, r = powers_of_2_in(n // 2)
>>>       return 1 + s, r
>>> --8<---------------cut here---------------end--------------->8---
>>
>> for n = 0 your function get stack overflow
> 
> That's right.  Let's say ``assert n > 0'' before we say ``if''.
> 

...or just:

...
     if n == 0 or remainder(n, 2) != 0:
...



More information about the Python-list mailing list