Minor fixes

- Add default number of iterations
- Correct "corner cases" to "edge cases"
- Make clear which bounds are inclusive and which are exclusive
This commit is contained in:
arthurvergacas
2021-10-15 12:21:48 -03:00
parent 09ac20dec2
commit ffb138adb1
2 changed files with 11 additions and 11 deletions

View File

@ -8,7 +8,7 @@
* However, there are certain numbers (so called Fermat Liars) that screw things up; * However, there are certain numbers (so called Fermat Liars) that screw things up;
* if a is one of these liars the equation will hold even though p is composite. * if a is one of these liars the equation will hold even though p is composite.
* *
* But not everything is lost! It's been proven that at least half of all integers aren't Fermat Liar (these ones called * But not everything is lost! It's been proven that at least half of all integers aren't Fermat Liars (these ones called
* Fermat Witnesses). Thus, if we keep testing the primality with random integers, we can achieve higher reliability. * Fermat Witnesses). Thus, if we keep testing the primality with random integers, we can achieve higher reliability.
* *
* The interesting about all of this is that since half of all integers are Fermat Witnesses, the precision gets really * The interesting about all of this is that since half of all integers are Fermat Witnesses, the precision gets really
@ -60,14 +60,14 @@ const modularExponentiation = (base, exponent, modulus) => {
* @param {number} numberOfIterations The number of times to apply Fermat's Little Theorem * @param {number} numberOfIterations The number of times to apply Fermat's Little Theorem
* @returns True if prime, false otherwise * @returns True if prime, false otherwise
*/ */
const fermatPrimeCheck = (n, numberOfIterations) => { const fermatPrimeCheck = (n, numberOfIterations = 50) => {
// first check for corner cases // first check for edge cases
if (n <= 1 || n === 4) return false if (n <= 1 || n === 4) return false
if (n <= 3) return true // 2 and 3 are included here if (n <= 3) return true // 2 and 3 are included here
for (let i = 0; i < numberOfIterations; i++) { for (let i = 0; i < numberOfIterations; i++) {
// pick a random number between 2 and n - 2 // pick a random number a, with 2 <= a < n - 2 (remember Math.random() range is [0, 1[ -> 1 exclusive)
const randomNumber = Math.floor(Math.random() * (n - 1 - 2) + 2) const randomNumber = Math.floor(Math.random() * (n - 2) + 2)
// if a^(n - 1) % n is different than 1, n is composite // if a^(n - 1) % n is different than 1, n is composite
if (modularExponentiation(randomNumber, n - 1, n) !== 1) { if (modularExponentiation(randomNumber, n - 1, n) !== 1) {

View File

@ -9,11 +9,11 @@ describe('modularExponentiation', () => {
describe('fermatPrimeCheck', () => { describe('fermatPrimeCheck', () => {
it('should give the correct output for prime and composite numbers', () => { it('should give the correct output for prime and composite numbers', () => {
expect(fermatPrimeCheck(2, 50)).toBe(true) expect(fermatPrimeCheck(2, 35)).toBe(true)
expect(fermatPrimeCheck(10, 50)).toBe(false) expect(fermatPrimeCheck(10, 30)).toBe(false)
expect(fermatPrimeCheck(94286167, 50)).toBe(true) expect(fermatPrimeCheck(94286167)).toBe(true)
expect(fermatPrimeCheck(83165867, 50)).toBe(true) expect(fermatPrimeCheck(83165867)).toBe(true)
expect(fermatPrimeCheck(13268774, 50)).toBe(false) expect(fermatPrimeCheck(13268774)).toBe(false)
expect(fermatPrimeCheck(13233852, 50)).toBe(false) expect(fermatPrimeCheck(13233852)).toBe(false)
}) })
}) })