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

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

Re: on a tail-recursive square-and-multiply

2023-11-09 Thread Julieta Shem via Python-list
Julieta Shem writes: [...] > I agree. By the way, I once read or watched an interview with Guido van > Rossum and and he was asked why not to tail-call optimize Python and the > answer he gave --- IIRC --- was that tail-call optimization makes it > harder for a beginner to understand a stack tr

Re: on a tail-recursive square-and-multiply

2023-11-08 Thread Julieta Shem via Python-list
Greg Ewing writes: > On 8/11/23 2:26 pm, Julieta Shem wrote: >> For the first time I'm trying to write a tail-recursive >> square-and-multiply and, even though it /seems/ to work, I'm not happy >> with what I wrote and I don't seem to understand it so well. > > Stepping back a bit, why do you fee

on a tail-recursive square-and-multiply

2023-11-07 Thread Julieta Shem via Python-list
For the first time I'm trying to write a tail-recursive square-and-multiply and, even though it /seems/ to work, I'm not happy with what I wrote and I don't seem to understand it so well. --8<---cut here---start->8--- def sam(b, e, m, acc = 1): if e == 0: