Skip to content

Commit fa1abf4

Browse files
Minor fixes
- Add some explanation and links about Carmichael Numbers - Remove explanation about in-built function Math.random()
1 parent ffb138a commit fa1abf4

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

Maths/FermatPrimalityTest.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
/*
2-
* The Fermat primality test is a probabilistic test to determine whether a number is a probable prime.
2+
* The Fermat primality test is a probabilistic test to determine whether a number
3+
* is a probable prime.
34
*
4-
* It relies on Fermat's Little Theorem, which states that if p is prime and a is not divisible by p, then
5+
* It relies on Fermat's Little Theorem, which states that if p is prime and a
6+
* is not divisible by p, then
57
*
68
* a^(p - 1) % p = 1
79
*
810
* However, there are certain numbers (so called Fermat Liars) that screw things up;
911
* if a is one of these liars the equation will hold even though p is composite.
1012
*
11-
* But not everything is lost! It's been proven that at least half of all integers aren't Fermat Liars (these ones called
12-
* Fermat Witnesses). Thus, if we keep testing the primality with random integers, we can achieve higher reliability.
13+
* But not everything is lost! It's been proven that at least half of all integers
14+
* aren't Fermat Liars (these ones called Fermat Witnesses). Thus, if we keep
15+
* testing the primality with random integers, we can achieve higher reliability.
1316
*
14-
* The interesting about all of this is that since half of all integers are Fermat Witnesses, the precision gets really
15-
* high really fast! Suppose that we make the test 50 times: the chance of getting only Fermat Liars in all runs is
17+
* The interesting about all of this is that since half of all integers are
18+
* Fermat Witnesses, the precision gets really high really fast! Suppose that we
19+
* make the test 50 times: the chance of getting only Fermat Liars in all runs is
1620
*
1721
* 1 / 2^50 = 8.8 * 10^-16 (a pretty small number)
1822
*
19-
* For comparison, the probability of a cosmic ray causing an error to your infalible program is around 1.4 * 10^-15. An
20-
* order of magnitude below!
23+
* For comparison, the probability of a cosmic ray causing an error to your
24+
* infalible program is around 1.4 * 10^-15. An order of magnitude below!
25+
*
26+
* But because nothing is perfect, there's a major flaw to this algorithm, and
27+
* the cause are the so called Carmichael Numbers. These are composite numbers n
28+
* that hold the equality from Fermat's Little Theorem for every a < n (excluding
29+
* is factors). In other words, if we are trying to determine if a Carmichael Number
30+
* is prime or not, the chances of getting a wrong answer are pretty high! Because
31+
* of that, the Fermat Primality Test is not used is serious applications. :(
2132
*
2233
* You can find more about the Fermat primality test and its flaws here:
2334
* https://en.wikipedia.org/wiki/Fermat_primality_test
35+
*
36+
* And about Carmichael Numbers here:
37+
* https://primes.utm.edu/glossary/xpage/CarmichaelNumber.html
2438
*/
2539

2640
/**
@@ -66,7 +80,7 @@ const fermatPrimeCheck = (n, numberOfIterations = 50) => {
6680
if (n <= 3) return true // 2 and 3 are included here
6781

6882
for (let i = 0; i < numberOfIterations; i++) {
69-
// pick a random number a, with 2 <= a < n - 2 (remember Math.random() range is [0, 1[ -> 1 exclusive)
83+
// pick a random number a, with 2 <= a < n - 2
7084
const randomNumber = Math.floor(Math.random() * (n - 2) + 2)
7185

7286
// if a^(n - 1) % n is different than 1, n is composite

0 commit comments

Comments
 (0)