-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prime.wat
57 lines (43 loc) · 1.6 KB
/
Prime.wat
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
45
46
47
48
49
50
51
52
53
54
55
56
57
(module
(func $even_check
(param $n i32)
(result i32)
(i32.eq ;; n % 2 === 0
(i32.rem_u (local.get $n) (i32.const 2))
(i32.const 0)
)
)
(func $multiple_check
(param $n i32)
(param $m i32)
(result i32)
(i32.eq ;; n % m === 0
(i32.rem_u (local.get $n) (local.get $m))
(i32.const 0)
)
)
(func $is_prime
(param $n i32)
(result i32)
(local $i i32)
(if (i32.eq (local.get $n) (i32.const 1)) ;; if n === 1 => false
(then (return (i32.const 0))))
(if (i32.eq (local.get $n) (i32.const 2)) ;; if n === 2 => true
(then (return (i32.const 1))))
(block $not_prime
(br_if $not_prime (call $even_check (local.get $n))) ;; if evenCheck(n) break block => false
(local.set $i (i32.const 1)) ;; i = 1
(loop $prime_test_loop
(local.set $i (i32.add (local.get $i) (i32.const 2))) ;; i = i + 2 (test next odd $i)
(if (i32.ge_u (local.get $i) (local.get $n)) ;; if i >= n => return true
(then (return (i32.const 1))))
(br_if $not_prime (call $multiple_check (local.get $n) (local.get $i))) ;; if isMultiple(n, i) break block => false
(br $prime_test_loop) ;; continue loop
)
)
(return (i32.const 0)) ;; return false
)
;; (export "EvenCheck" (func $even_check))
;; (export "MultipleCheck" (func $multiple_check))
(export "IsPrime" (func $is_prime))
)