blob: 326a5d5d49f461e98431a94d96b73161192d5c98 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
module sieve
// The standard Sieve of Eratosthenes.
//import StdEnv
(+) infixl 6 :: !Int !Int -> Int
(+) a b = code inline {
addI
}
(-) infixl 6 :: !Int !Int -> Int
(-) a b = code inline {
subI
}
(==) infix 4 :: !Int !Int -> Bool
(==) a b = code inline {
eqI
}
(rem) infix 7 :: !Int !Int -> Int
(rem) a b = code inline {
remI
}
fro :: !Int -> [Int]
fro i = [i:fro (i+1)]
take :: Int [a] -> [a]
take 0 _ = []
take n [x:xs] = [x:take (n-1) xs]
NrOfPrimes :== 1000
// The sieve algorithm: generate an infinite list of all primes.
Start = take NrOfPrimes (sieve (fro 2))
where
sieve [prime:rest] = [prime : sieve (filter prime rest)]
filter p [h:tl] | h rem p == 0 = filter p tl
= [h : filter p tl]
filter p [] = []
|