Skip to content

Instantly share code, notes, and snippets.

@anabastos
Last active January 7, 2020 00:16
Show Gist options
  • Save anabastos/d33e5c850d6250399c0d731ed2fb4b84 to your computer and use it in GitHub Desktop.
Save anabastos/d33e5c850d6250399c0d731ed2fb4b84 to your computer and use it in GitHub Desktop.
Dado um número aleatório, retornar todos os números PRIMOS entre 0 e o número escolhido
const criaArrayPrimos = x =>
criaArray(x)
.slice(2)
.filter(checaFatores)
const criaArray = tamanho => Array.from({ length: tamanho }, (el, index) => index)
const checaFatores = n =>
criaArray(maiorDivisor(n))
.slice(2)
.every(m => n % m !== 0)
const maiorDivisor = x => Math.ceil(Math.sqrt(x + 1))
console.log(criaArrayPrimos(40)) //[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment