From c5e44d498ac5433099483677375abe49d6baf21e Mon Sep 17 00:00:00 2001 From: Roland Hummel Date: Thu, 14 Oct 2021 22:32:56 +0200 Subject: [PATCH] A few suggestions / modifications / fixes It seems you've accidentally swapped the implementation and the test file :) The overall comment describing the algorithm (VERY nice doc, by the way) is not "proper" JSdoc => only one leading asterisk. It's generally considered good style to start a comment block (both JSdoc and regular comments) with a single, short sentence. Further down, there were some git hiccups, most likely caused by merge conflicts? --- Maths/{test => }/FermatPrimalityTest.js | 17 +++++------------ Maths/{ => test}/FermatPrimalityTest.test.js | 2 +- 2 files changed, 6 insertions(+), 13 deletions(-) rename Maths/{test => }/FermatPrimalityTest.js (86%) rename Maths/{ => test}/FermatPrimalityTest.test.js (91%) diff --git a/Maths/test/FermatPrimalityTest.js b/Maths/FermatPrimalityTest.js similarity index 86% rename from Maths/test/FermatPrimalityTest.js rename to Maths/FermatPrimalityTest.js index 144816348..3b8c5661f 100644 --- a/Maths/test/FermatPrimalityTest.js +++ b/Maths/FermatPrimalityTest.js @@ -1,6 +1,7 @@ -/** - * The Fermat primality test is a probabilistic test to determine whether a number is a probable prime. It relies on - * Fermat's Little Theorem, which states that if p is prime and a is not divisible by p, then +/* + * The Fermat primality test is a probabilistic test to determine whether a number is a probable prime. + * + * It relies on Fermat's Little Theorem, which states that if p is prime and a is not divisible by p, then * * a^(p - 1) % p = 1 * @@ -53,7 +54,7 @@ const modularExponentiation = (base, exponent, modulus) => { } /** - * Test if a given number n is prime or not. + * Test if a given number n is prime or not. * * @param {number} n The number to check for primality * @param {number} numberOfIterations The number of times to apply Fermat's Little Theorem @@ -61,20 +62,12 @@ const modularExponentiation = (base, exponent, modulus) => { */ const fermatPrimeCheck = (n, numberOfIterations) => { // first check for corner cases -<<<<<<< HEAD if (n <= 1 || n === 4) return false -======= - if (n <= 1 || n == 4) return false ->>>>>>> 951c7258323a057041c0d128880982ddab303ee5 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 -<<<<<<< HEAD const randomNumber = Math.floor(Math.random() * (n - 1 - 2) + 2) -======= - let randomNumber = Math.floor(Math.random() * (n - 1 - 2) + 2) ->>>>>>> 951c7258323a057041c0d128880982ddab303ee5 // if a^(n - 1) % n is different than 1, n is composite if (modularExponentiation(randomNumber, n - 1, n) !== 1) { diff --git a/Maths/FermatPrimalityTest.test.js b/Maths/test/FermatPrimalityTest.test.js similarity index 91% rename from Maths/FermatPrimalityTest.test.js rename to Maths/test/FermatPrimalityTest.test.js index 04b1ff402..d6605e079 100644 --- a/Maths/FermatPrimalityTest.test.js +++ b/Maths/test/FermatPrimalityTest.test.js @@ -1,4 +1,4 @@ -import { modularExponentiation, fermatPrimeCheck } from '../FermatPrimalityTest' +import { fermatPrimeCheck, modularExponentiation } from '../FermatPrimalityTest' describe('modularExponentiation', () => { it('should give the correct output for all exponentiations', () => {