|
1 |
| -/** |
2 |
| - * The Fermat primality test is a probabilistic test to determine whether a number is a probable prime. It relies on |
3 |
| - * Fermat's Little Theorem, which states that if p is prime and a is not divisible by p, then |
| 1 | +/* |
| 2 | + * The Fermat primality test is a probabilistic test to determine whether a number is a probable prime. |
| 3 | + * |
| 4 | + * It relies on Fermat's Little Theorem, which states that if p is prime and a is not divisible by p, then |
4 | 5 | *
|
5 | 6 | * a^(p - 1) % p = 1
|
6 | 7 | *
|
@@ -53,28 +54,20 @@ const modularExponentiation = (base, exponent, modulus) => {
|
53 | 54 | }
|
54 | 55 |
|
55 | 56 | /**
|
56 |
| - * Test if a given number n is prime or not. |
| 57 | + * Test if a given number n is prime or not. |
57 | 58 | *
|
58 | 59 | * @param {number} n The number to check for primality
|
59 | 60 | * @param {number} numberOfIterations The number of times to apply Fermat's Little Theorem
|
60 | 61 | * @returns True if prime, false otherwise
|
61 | 62 | */
|
62 | 63 | const fermatPrimeCheck = (n, numberOfIterations) => {
|
63 | 64 | // first check for corner cases
|
64 |
| -<<<<<<< HEAD |
65 | 65 | if (n <= 1 || n === 4) return false
|
66 |
| -======= |
67 |
| - if (n <= 1 || n == 4) return false |
68 |
| ->>>>>>> 951c7258323a057041c0d128880982ddab303ee5 |
69 | 66 | if (n <= 3) return true // 2 and 3 are included here
|
70 | 67 |
|
71 | 68 | for (let i = 0; i < numberOfIterations; i++) {
|
72 | 69 | // pick a random number between 2 and n - 2
|
73 |
| -<<<<<<< HEAD |
74 | 70 | const randomNumber = Math.floor(Math.random() * (n - 1 - 2) + 2)
|
75 |
| -======= |
76 |
| - let randomNumber = Math.floor(Math.random() * (n - 1 - 2) + 2) |
77 |
| ->>>>>>> 951c7258323a057041c0d128880982ddab303ee5 |
78 | 71 |
|
79 | 72 | // if a^(n - 1) % n is different than 1, n is composite
|
80 | 73 | if (modularExponentiation(randomNumber, n - 1, n) !== 1) {
|
|
0 commit comments