Skip to content

Instantly share code, notes, and snippets.

@ymulenll
Created September 7, 2022 14:51
Show Gist options
  • Save ymulenll/8d6eb64eac34c446980b70e7cc7c509f to your computer and use it in GitHub Desktop.
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
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