エラトステネスの篩 - JavaScript版 at HouseTect, JavaScriptな情報をあなたに をGroovyで。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| #!/usr/bin/env groovy
def findAllPrime(int max) {
assert max >= 2
def list = 2..max
def primeList = []
while (!primeList || (list && list.max() >= primeList.max()**2)) {
def checkNum = list[0]
primeList << checkNum
list = list.findAll { it % checkNum != 0 }
}
(list + primeList).sort()
}
assert findAllPrime(2) == [2]
assert findAllPrime(10) == [2, 3, 5, 7]
assert findAllPrime(20) == [2, 3, 5, 7, 11, 13, 17, 19]
|
今日は仕事休み。長い間たまった振替休日を消化。