on a very slow function

Daniel Bastos dbastos at toledo.com
Mon Oct 2 13:21:38 EDT 2017


Ben Bacarisse <ben.usenet at bsb.me.uk> writes:

> Daniel Bastos <dbastos at toledo.com> writes:
>
>> def make_sequence_non_recursive(N, x0 = 2, c = -1):
>>   "What's wrong with this function?  It's very slow."
>>   last = x0
>>   def sequence():
>>     nonlocal last
>>     next = last
>>     last = last**2 + c
>>     return next % N
>>   return sequence
>>
>> It crawls pretty soon.  Please advise?
>
> A mathematical rather than Python answer... change it to
>
>       last = (last**2 + c) % N
>       return next

Amazing!  That was an accident.  Thanks for pointing that out!



More information about the Python-list mailing list