Created
September 7, 2022 14:51
-
-
Save ymulenll/8d6eb64eac34c446980b70e7cc7c509f to your computer and use it in GitHub Desktop.
Obtener rango de números primos - Ejemplo para el video https://youtu.be/BPWpialFWvM
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
const primos = obtenerNumerosPrimos(1, 10); | |
console.log(primos); | |
function obtenerNumerosPrimos(numeroInicial, numeroFinal) { | |
const numerosPrimos = []; | |
if (numeroInicial > numeroFinal) { | |
throw Error("El número inicial debe ser menor que el número final"); | |
} | |
for (let actual = numeroInicial; actual <= numeroFinal; actual++) { | |
if (esPrimo(actual)) { | |
numerosPrimos.push(actual); | |
} | |
} | |
return numerosPrimos; | |
} | |
function esPrimo(numero) { | |
if (numero <= 2) return false; | |
for (let i = 2; i <= Math.sqrt(numero); i++) { | |
if (numero % i === 0) { | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment