From b95dab4f053e9a62b94bd4d77eb0f46c3f3e5d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cesar=20Daniel=20Pi=C3=B1a=20Gonzalez?= Date: Fri, 17 Nov 2023 19:05:03 -0600 Subject: [PATCH] Reto #21 - Javascript --- .../javascript/Cesarpinagon.js" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 "Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/javascript/Cesarpinagon.js" diff --git "a/Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/javascript/Cesarpinagon.js" "b/Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/javascript/Cesarpinagon.js" new file mode 100644 index 0000000000..b5ebf85be8 --- /dev/null +++ "b/Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/javascript/Cesarpinagon.js" @@ -0,0 +1,17 @@ +function isPrime(num) { + for(let i = 2, sqrt = Math.sqrt(num); i <= sqrt; i++) + if(num % i === 0) return false; + return num > 1; +} + +function findTwinPrimes(n) { + let twinPrimes = []; + for(let i = 2; i <= n - 2; i++) { + if(isPrime(i) && isPrime(i + 2)) { + twinPrimes.push([i, i + 2]); + } + } + return twinPrimes; +} + +console.log(findTwinPrimes(14)); \ No newline at end of file