Fermat’s Little Theorem
Thankfully, there are good Monte Carlo algorithms for primality testing. A number of them revolve around a theorem first published back in the year 1640.
Pierre de Fermat, a 17th-century French mathematician, came up with a number of mathematical theorems in his lifetime, and the one relevant to us is known as Fermat’s Little Theorem. (Yes, it’s actually called that, and don’t ask me why.)
Fermat’s Little Theorem can be expressed in a number of ways, but I’ll present it in the way that I feel is clearest for our context. Here goes.
Let’s use the variable N to refer to a number we’re testing for being prime. Every number N has a series of smaller numbers that run from 1 up to N, excluding N itself. We’re going to use ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access