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

jak nospam at please.ty
Wed Nov 29 23:58:34 EST 2023


Dom Grigonis ha scritto:
> def powers_of_2_in(n):
>      s = 0
>      while n % 2 == 0:
>          s += 1
>          n = n // 2
>      return s, n
> 

Good solution, unfortunately if the input data is zero, the function
never ends.

>> On 30 Nov 2023, at 02:44, Julieta Shem via Python-list <python-list at python.org> wrote:
>>
>> How would you write this procedure?
>>
>> --8<---------------cut here---------------start------------->8---
>> def powers_of_2_in(n):
>>   s = 0
>>   while "I still find factors of 2 in n...":
>>     q, r = divmod(n, 2)
>>     if r == 0:
>>       s = s + 1
>>       n = n // 2
>>     else:
>>       return s, n
>> --8<---------------cut here---------------end--------------->8---
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list
> 



More information about the Python-list mailing list