From ffb138adb18df3b947ee49fd232f6fc155debb05 Mon Sep 17 00:00:00 2001 From: arthurvergacas Date: Fri, 15 Oct 2021 12:21:48 -0300 Subject: [PATCH] Minor fixes - Add default number of iterations - Correct "corner cases" to "edge cases" - Make clear which bounds are inclusive and which are exclusive --- Maths/FermatPrimalityTest.js | 10 +++++----- Maths/test/FermatPrimalityTest.test.js | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Maths/FermatPrimalityTest.js b/Maths/FermatPrimalityTest.js index 3b8c5661f..aa79e4013 100644 --- a/Maths/FermatPrimalityTest.js +++ b/Maths/FermatPrimalityTest.js @@ -8,7 +8,7 @@ * 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. * - * 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. * * 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 * @returns True if prime, false otherwise */ -const fermatPrimeCheck = (n, numberOfIterations) => { - // first check for corner cases +const fermatPrimeCheck = (n, numberOfIterations = 50) => { + // first check for edge cases if (n <= 1 || n === 4) return false if (n <= 3) return true // 2 and 3 are included here for (let i = 0; i < numberOfIterations; i++) { - // pick a random number between 2 and n - 2 - const randomNumber = Math.floor(Math.random() * (n - 1 - 2) + 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 - 2) + 2) // if a^(n - 1) % n is different than 1, n is composite if (modularExponentiation(randomNumber, n - 1, n) !== 1) { diff --git a/Maths/test/FermatPrimalityTest.test.js b/Maths/test/FermatPrimalityTest.test.js index d6605e079..92fad4bcd 100644 --- a/Maths/test/FermatPrimalityTest.test.js +++ b/Maths/test/FermatPrimalityTest.test.js @@ -9,11 +9,11 @@ describe('modularExponentiation', () => { describe('fermatPrimeCheck', () => { it('should give the correct output for prime and composite numbers', () => { - expect(fermatPrimeCheck(2, 50)).toBe(true) - expect(fermatPrimeCheck(10, 50)).toBe(false) - expect(fermatPrimeCheck(94286167, 50)).toBe(true) - expect(fermatPrimeCheck(83165867, 50)).toBe(true) - expect(fermatPrimeCheck(13268774, 50)).toBe(false) - expect(fermatPrimeCheck(13233852, 50)).toBe(false) + expect(fermatPrimeCheck(2, 35)).toBe(true) + expect(fermatPrimeCheck(10, 30)).toBe(false) + expect(fermatPrimeCheck(94286167)).toBe(true) + expect(fermatPrimeCheck(83165867)).toBe(true) + expect(fermatPrimeCheck(13268774)).toBe(false) + expect(fermatPrimeCheck(13233852)).toBe(false) }) })