From cbdb9580691a34aa32d1e627a46d129a64e66014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Wed, 5 Oct 2022 00:23:17 -0400 Subject: [PATCH 1/4] Update checkPrime.js --- algorithms/math/checkPrime.js | 38 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/algorithms/math/checkPrime.js b/algorithms/math/checkPrime.js index bf2f4b8..757ace6 100644 --- a/algorithms/math/checkPrime.js +++ b/algorithms/math/checkPrime.js @@ -1,25 +1,27 @@ -// JavaScript implementation of prime number checking -// -// Author: Nikolas Huebecker +/** + * JS implementation of primality testing + * @param {number} n + * @author Nikolas Huebecker +*/ +const isPrime = n => { + n = Math.abs(n); -function isPrime(n){ - var divisor = 2; + if (!Number.isInteger(n)) + return false; + + if (n % 2 === 0) + return n === 2; + + const rt = Math.sqrt(n); + + for (let d = 3; d <= rt; d += 2) + if (n % d === 0) + return false; - while (n > divisor){ - if(n % divisor == 0){ - return false; - }else { - divisor++; - } - } return true; } // Test -console.log("The number 137 is prime:" -console.log(isPrime(137)) - - -console.log("The number 16 is prime:" -console.log(isPrime(16)) +for (const n of [137, 16, 24, 127, 11]) + console.log(`The number ${n} is prime: ${isPrime(n)}`) From d45ff367d1ef94b5047b0ec2ae8714e0976624f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Wed, 5 Oct 2022 00:26:55 -0400 Subject: [PATCH 2/4] Enable strict mode --- algorithms/math/checkPrime.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/algorithms/math/checkPrime.js b/algorithms/math/checkPrime.js index 757ace6..6405d96 100644 --- a/algorithms/math/checkPrime.js +++ b/algorithms/math/checkPrime.js @@ -1,3 +1,5 @@ +"use strict"; + /** * JS implementation of primality testing * @param {number} n From 4fda49b462d9affb5ac08e897eb213ddf8c8ada8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Wed, 5 Oct 2022 00:34:09 -0400 Subject: [PATCH 3/4] Update checkPrime.js --- algorithms/math/checkPrime.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/algorithms/math/checkPrime.js b/algorithms/math/checkPrime.js index 6405d96..e89dcd8 100644 --- a/algorithms/math/checkPrime.js +++ b/algorithms/math/checkPrime.js @@ -6,11 +6,11 @@ * @author Nikolas Huebecker */ const isPrime = n => { - n = Math.abs(n); - if (!Number.isInteger(n)) return false; + n = Math.abs(n); + if (n % 2 === 0) return n === 2; From aaca0c7539cbd994b769fe17c2bc2b856aef40e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Wed, 5 Oct 2022 00:37:09 -0400 Subject: [PATCH 4/4] Update checkPrime.js --- algorithms/math/checkPrime.js | 1 + 1 file changed, 1 insertion(+) diff --git a/algorithms/math/checkPrime.js b/algorithms/math/checkPrime.js index e89dcd8..66f0a60 100644 --- a/algorithms/math/checkPrime.js +++ b/algorithms/math/checkPrime.js @@ -4,6 +4,7 @@ * JS implementation of primality testing * @param {number} n * @author Nikolas Huebecker + * @author Ricardo Fernández Serrata */ const isPrime = n => { if (!Number.isInteger(n))