-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsieve_test.cw
More file actions
33 lines (27 loc) · 789 Bytes
/
sieve_test.cw
File metadata and controls
33 lines (27 loc) · 789 Bytes
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
; prime number sieve
module:
import test
global SIZE uint = 1000000
global EXPECTED uint = 148932
; The array is initialized to all true because the explicit
; value for the first element is replicated for the
; subsequent unspecified ones.
;
; index i reprents number 3 + 2 * i
global! is_prime = {[SIZE]bool: true}
; the actual sieve function
fun sieve() uint:
; mutable local variable
let! count uint = 0
; the type of loop variable `i` is determined by `N`
for i = 0, SIZE, 1:
if is_prime[i]:
set count += 1
let p uint = i + i + 3
for k = i + p, SIZE, p:
set is_prime[k] = false
return count
fun main(argc s32, argv ^^u8) s32:
test\AssertEq#(sieve(), EXPECTED)
test\Success#()
return 0