arithmetic-operations-for / integers-modulo-n-big-endian

:mermaid: Modular arithmetic for JavaScript
https://arithmetic-operations-for.github.io/integers-modulo-n-big-endian
GNU Affero General Public License v3.0
0 stars 1 forks source link

Modular arithmetic for Mersenne numbers #6

Open make-github-pseudonymous-again opened 4 years ago

make-github-pseudonymous-again commented 4 years ago

Let r be the radix of representation, then computing x % r^k-1 can be reduced using the following identity (with R = r^k):

x = x % R + x // R (mod R-1)

See https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test#Time_complexity

make-github-pseudonymous-again commented 4 years ago

This also works for numbers of the form R-c:

Let x = hi * R + lo, then x = hi * (R-c) + c * hi + lo = c * hi + lo (mod R-c).