on a very slow function

Ian Kelly ian.g.kelly at gmail.com
Mon Oct 2 13:23:18 EDT 2017


On Sun, Oct 1, 2017 at 8:14 PM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
>
> On Mon, 2 Oct 2017 12:00 pm, Ben Bacarisse wrote:
>
>
> >> Better:
> >>
> >> last = (pow(last, 2, N) + (2 % N)) % N
> >
> > You meant c rather than 2, I think.
>
> Oops, yes, that was a typo.
>
>
> > And I'm not convinced all the %Ns
> > are worth while.
>
> They are all necessary.
>
>
> py> (2**75 + 7) % 12  # Expected value.
> 3
> py> ((2**75) % 12 + (7 % 12)) % 12  # Correct.
> 3
> py> (2**75) % 12 + (7 % 12)  # Incorrect.
> 15

No, only the final one is necessary. Modding the result of the
exponentiation might be useful for performance, but if c is expected
to be small then it may be pointless to mod that as well.

py> ((2**75) % 12 + 7) % 12  # Still correct.
3



More information about the Python-list mailing list