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

2023-12-05 Thread jak via Python-list
Alan Bawden ha scritto: If you like this sort of stuff, check out the book "Hacker's Delight" by Henry Warren. See. Thank you for your suggestion. Really interesting. Just for fun I tried to port the function to 64 bit: def bit_count_64(n):

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

2023-12-05 Thread Alan Bawden via Python-list
jak writes: Oscar Benjamin ha scritto: ... If we now use the function being discussed: powers_of_2_in(n) (63, 1) we can see that the bit_count() method had to do 63 iterations to count the bits I certainly hope that the bit_count method doesn't count bits by iterating

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

2023-12-04 Thread jak via Python-list
Oscar Benjamin ha scritto: On Sun, 3 Dec 2023 at 10:25, Julieta Shem via Python-list wrote: Alan Bawden writes: def powers_of_2_in(n): bc = (n ^ (n - 1)).bit_count() - 1 return bc, n >> bc That's pretty fancy and likely the fastest. It might be the fastest but it depends how b

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

2023-12-03 Thread Oscar Benjamin via Python-list
On Sun, 3 Dec 2023 at 10:25, Julieta Shem via Python-list wrote: > > Alan Bawden writes: > > > > def powers_of_2_in(n): > > bc = (n ^ (n - 1)).bit_count() - 1 > > return bc, n >> bc > > That's pretty fancy and likely the fastest. It might be the fastest but it depends how big you expect

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

2023-12-03 Thread jak via Python-list
Julieta Shem ha scritto: jak 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-

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

2023-12-03 Thread Julieta Shem via Python-list
jak 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--

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

2023-12-03 Thread jak via Python-list
Julieta Shem ha scritto: Alan Bawden writes: 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

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

2023-12-03 Thread Julieta Shem via Python-list
Alan Bawden writes: > 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 >> >

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

2023-11-29 Thread Alan Bawden via Python-list
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 va

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

2023-11-29 Thread jak via Python-list
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 y

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

2023-11-29 Thread jak via Python-list
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?

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

2023-11-29 Thread Alan Bawden via Python-list
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

2023-11-29 Thread Dan Sommers via Python-list
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

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

2023-11-29 Thread Dom Grigonis via Python-list
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_

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

2023-11-29 Thread Julieta Shem via Python-list
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<---c