Fermat’s Primality Test
Remember our Bad Primality Testing algorithm? We chose 100 random numbers and divided a number by them to see if number was prime. This turned out to be a losing proposition. However, we can use a similar tactic that utilizes Fermat’s Little Theorem. This approach is called Fermat’s primality testing since it’s predicated upon Fermat’s Little Theorem. I’m going to unfold this algorithm one logical layer at a time, so bear with me.
We’ve already derived logically from Fermat’s Little Theorem that if we take a number N and run it through The Formula by plugging in some example of A and do not get a result of 1, then N must be composite.
Based on this, we can pick 100 random numbers to plug into the A variable, and run each ...
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