|
1 | 1 | /*
|
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. |
3 | 4 | *
|
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 |
5 | 7 | *
|
6 | 8 | * a^(p - 1) % p = 1
|
7 | 9 | *
|
8 | 10 | * However, there are certain numbers (so called Fermat Liars) that screw things up;
|
9 | 11 | * if a is one of these liars the equation will hold even though p is composite.
|
10 | 12 | *
|
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. |
13 | 16 | *
|
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 |
16 | 20 | *
|
17 | 21 | * 1 / 2^50 = 8.8 * 10^-16 (a pretty small number)
|
18 | 22 | *
|
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. :( |
21 | 32 | *
|
22 | 33 | * You can find more about the Fermat primality test and its flaws here:
|
23 | 34 | * https://en.wikipedia.org/wiki/Fermat_primality_test
|
| 35 | + * |
| 36 | + * And about Carmichael Numbers here: |
| 37 | + * https://primes.utm.edu/glossary/xpage/CarmichaelNumber.html |
24 | 38 | */
|
25 | 39 |
|
26 | 40 | /**
|
@@ -66,7 +80,7 @@ const fermatPrimeCheck = (n, numberOfIterations = 50) => {
|
66 | 80 | if (n <= 3) return true // 2 and 3 are included here
|
67 | 81 |
|
68 | 82 | 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 |
70 | 84 | const randomNumber = Math.floor(Math.random() * (n - 2) + 2)
|
71 | 85 |
|
72 | 86 | // if a^(n - 1) % n is different than 1, n is composite
|
|
0 commit comments