on a very slow function

Chris Angelico rosuav at gmail.com
Sun Oct 1 17:35:24 EDT 2017


On Mon, Oct 2, 2017 at 8:27 AM, Daniel Bastos <dbastos at toledo.com> wrote:
> 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?  Thank you.

For a start, it should probably be implemented as a generator. I'm not
sure what this is even trying to do.

For seconds, the variable names could do with some improvement. Ditto.

ChrisA



More information about the Python-list mailing list