-
Notifications
You must be signed in to change notification settings - Fork 1
/
prime.j
142 lines (103 loc) · 1.55 KB
/
prime.j
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
.class public prime
.super java/lang/Object
;; Constructor for class prime
.method public <init>()V
.limit stack 1
aload_0
invokespecial java/lang/Object/<init>()V
return
.end method
;; Default Java entrypoint: void main (String [])
.method public static main([Ljava/lang/String;)V
.limit stack 1
invokestatic prime/main()I
pop
return
.end method
;; C-- entrypoint: int main()
.method public static main()I
.limit locals 0
.limit stack 1
;; if (prime (641))
ldc 641
invokestatic prime/prime(I)Z
ifne L0
;; printInt (0);
iconst_0
invokestatic Runtime/printInt(I)V
goto L1
L0:
;; printInt (641);
ldc 641
invokestatic Runtime/printInt(I)V
L1:
iconst_0
ireturn
.end method
.method public static divides(II)Z
.limit locals 2
.limit stack 2
;; return p / q * q == p;
iload_1 ;; p
iload_0 ;; q
idiv
iload_0
imul
iload_1
if_icmpeq L0
iconst_0
goto L1
L0:
iconst_1
L1:
ireturn
.end method
.method public static prime(I)Z
.limit locals 2
.limit stack 2
;; if (p <= 1 || divides (2, p))
iload_0 ;; p
iconst_1
if_icmple L4
iconst_2
iload_0
invokestatic prime/divides(II)Z
ifne L4
;; int q = 3;
iconst_3
istore_1
;; while (q * q <= p)
goto L3
L0:
;; if (divides (q, p))
iload_1
iload_0
invokestatic prime/divides(II)Z
ifne L1
;; q = q + 2;
iload_1
iconst_2
iadd
istore_1
goto L2
L1:
;; return false;
iconst_0
ireturn
L2:
L3:
iload_1
iload_1
imul
iload_0
if_icmple L0
goto L5
L4:
;; return false;
iconst_0
ireturn
L5:
;; return true;
iconst_1
ireturn
.end method