From 61c0db06567b6dbbd7d715107e8d402c492a42fd Mon Sep 17 00:00:00 2001 From: devartes Date: Sun, 23 Oct 2022 09:52:13 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20adicionando=20solu=C3=A7=C3=A3o=20do=20?= =?UTF-8?q?5=C2=B0=20desafio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05-é-primo/index.html | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/05-é-primo/index.html b/05-é-primo/index.html index cd11c0c..e7ad135 100644 --- a/05-é-primo/index.html +++ b/05-é-primo/index.html @@ -16,6 +16,26 @@ */ function isPrime(number) { // implementar logica aqui + if (number === 2 || number === 3) return true + if (number % 2 === 0 || number < 2) return false + + var s = 0, + d = number - 1 + while ((d & 1) == 0) { + d >>= 1 + ++s + } + + let base = 2 + var x = Math.pow(base, d) % number + + if (x == 1 || x == number - 1) return true + + for (var i = 1; i <= s; i++) { + x = (x * x) % number + + if (x === number - 1) return true + } return false }