-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3556 from nlarrea/js-branch
Reto #21 - javascript
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
Retos/Reto #21 - NÚMEROS PRIMOS GEMELOS [Media]/javascript/nlarrea.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Crea un programa que encuentre y muestre todos los pares de números primos | ||
* gemelos en un rango concreto. | ||
* El programa recibirá el rango máximo como número entero positivo. | ||
* | ||
* - Un par de números primos se considera gemelo si la diferencia entre | ||
* ellos es exactamente 2. Por ejemplo (3, 5), (11, 13) | ||
* | ||
* - Ejemplo: Rango 14 | ||
* (3, 5), (5, 7), (11, 13) | ||
*/ | ||
|
||
|
||
function getPairsOfPrimes(range) { | ||
let pairsOfPrimes = []; | ||
let pair = []; | ||
|
||
for (let i=2; i<=range; i++) { | ||
if (isPrime(i)) { | ||
pair.push(i); | ||
if (pair.length === 2) { | ||
if (pair[1] - pair[0] === 2) { | ||
pairsOfPrimes.push(pair.slice()); | ||
} | ||
|
||
pair.shift(); | ||
} | ||
} | ||
} | ||
|
||
return pairsOfPrimes; | ||
} | ||
|
||
|
||
const isPrime = (number) => { | ||
if (number < 2) return false; | ||
|
||
for (let i=2; i<number; i++) { | ||
if (number % i === 0) return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
function printPairs(pairs) { | ||
console.log('\nPairs of prime numbers:'); | ||
for (let pair of pairs) { | ||
console.log(pair); | ||
} | ||
} | ||
|
||
|
||
printPairs(getPairsOfPrimes(14)); |