Skip to content

Instantly share code, notes, and snippets.

@takahiro47
Created August 1, 2014 06:39
Show Gist options
  • Save takahiro47/2d040de7559fecf74885 to your computer and use it in GitHub Desktop.
Save takahiro47/2d040de7559fecf74885 to your computer and use it in GitHub Desktop.
エラトステネスの篩のワンライナー
## Solution 1
x = 1000
primes = []
primes.push i for i in [2..x] when not (j for j in primes when i % j == 0).length
## Solution 2
# 洗練する前
# (n) -> (p.push i for i in [2..n] when not (j for j in (p or p=[]) when i%j == 0)[0]) and n in p
# 完成形
(n) -> (p.push i for i in [2..n] when !(p or p=[]).some((j) -> i%j is 0)) and n in p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment