Skip to content

Instantly share code, notes, and snippets.

@lubien
Forked from Woodsphreaker/primeNumbers.js
Last active March 14, 2017 19:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lubien/b984b501ea2df1242df34cd5bc75b4ca to your computer and use it in GitHub Desktop.
Save lubien/b984b501ea2df1242df34cd5bc75b4ca 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
range = x => y =>
Array.from({length: y - x})
.map((_, i) => x + i)
, {ceil, sqrt} = Math
, lt = y => x =>
x < y
, takeWhile = pred => it => {
let val, xs = []
while(pred(val = it.next().value)) {
xs.push(val)
}
return xs
}
, some = pred => xs =>
xs.some(pred)
, not = prop =>
!prop
, divides = x => y =>
x % y === 0
, prime = x =>
not(some(divides(x))(range(2)(ceil(sqrt(x + 1)))))
, primes = function * (n) {
let i = 2
do { if (prime(i)) { yield i } } while (i++)
}
takeWhile(lt(100))(primes())
@Woodsphreaker
Copy link

Demais !!!!

@lubien
Copy link
Author

lubien commented Mar 14, 2017

Revision inspired by @anabastos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment