on writing a number as 2^s * q, where q is odd
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
Re: on writing a number as 2^s * q, where q is odd
def powers_of_2_in(n): s = 0 while n % 2 == 0: s += 1 n = n // 2 return s, n > On 30 Nov 2023, at 02:44, Julieta Shem via Python-list > 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: on writing a number as 2^s * q, where q is odd
On 2023-11-29 at 21:44:01 -0300, Julieta Shem via Python-list 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--- What's wrong with what you have? -- https://mail.python.org/mailman/listinfo/python-list
Re: on writing a number as 2^s * q, where q is odd
Julieta Shem writes: How would you write this procedure? def powers_of_2_in(n): ... def powers_of_2_in(n): return (n ^ (n - 1)).bit_count() - 1 -- https://mail.python.org/mailman/listinfo/python-list
Re: on writing a number as 2^s * q, where q is odd
Alan Bawden ha scritto: Julieta Shem writes: How would you write this procedure? def powers_of_2_in(n): ... def powers_of_2_in(n): return (n ^ (n - 1)).bit_count() - 1 Great solution, unfortunately the return value is not a tuple as in the OP version. Maybe in this way? def powers_of_2_inB(n): bc = (n ^ (n - 1)).bit_count() - 1 return bc, int(n / (1 << bc)) -- https://mail.python.org/mailman/listinfo/python-list
Re: on writing a number as 2^s * q, where q is odd
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 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: on writing a number as 2^s * q, where q is odd
jak writes: Alan Bawden ha scritto: > Julieta Shem writes: > > How would you write this procedure? > def powers_of_2_in(n): > ... > > def powers_of_2_in(n): > return (n ^ (n - 1)).bit_count() - 1 > Great solution, unfortunately the return value is not a tuple as in the OP version. Maybe in this way? def powers_of_2_inB(n): bc = (n ^ (n - 1)).bit_count() - 1 return bc, int(n / (1 << bc)) Good point. I overlooked that. I should have written: def powers_of_2_in(n): bc = (n ^ (n - 1)).bit_count() - 1 return bc, n >> bc -- https://mail.python.org/mailman/listinfo/python-list
