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

Julieta Shem jshem at yaxenu.org
Sun Dec 3 00:08:39 EST 2023


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''.


More information about the Python-list mailing list