on a very slow function

Steve D'Aprano steve+python at pearwood.info
Tue Oct 3 23:51:36 EDT 2017


On Tue, 3 Oct 2017 04:23 am, Ian Kelly wrote:

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

Its been years since I've had to tackle modulo arithmetic, but the more I
think about it, the more I think you are right.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list