osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

on a very slow function


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