@@ -131,19 +131,19 @@ u64 binpower(u64 base, u64 e, u64 mod) {
131131 return result;
132132}
133133
134- bool trial_composite (u64 n, u64 a, u64 d, int s) {
134+ bool check_composite (u64 n, u64 a, u64 d, int s) {
135135 u64 x = binpower(a, d, n);
136136 if (x == 1 || x == n - 1)
137- return true ;
137+ return false ;
138138 for (int r = 1; r < s; r++) {
139139 x = (u128)x * x % n;
140140 if (x == n - 1)
141- return true ;
141+ return false ;
142142 }
143- return false ;
143+ return true ;
144144};
145145
146- bool MillerRabin(u64 n) {
146+ bool MillerRabin(u64 n) { // returns true if n is probably prime, else returns false.
147147 if (n < 4)
148148 return n == 2 || n == 3;
149149
@@ -156,7 +156,7 @@ bool MillerRabin(u64 n) {
156156
157157 for (int i = 0; i < iter; i++) {
158158 int a = 2 + rand() % (n - 3);
159- if (trial_composite (n, a, d, s))
159+ if (check_composite (n, a, d, s))
160160 return false;
161161 }
162162 return true;
@@ -175,13 +175,13 @@ Bach later gave a concrete bound, it is only necessary to test all bases $a \le
175175This is still a pretty large number of bases.
176176So people have invested quite a lot of computation power into finding lower bounds.
177177It turns out, for testing a 32 bit integer it is only necessary to check the first 4 prime bases: 2, 3, 5 and 7.
178- The smallest composite number that fails this test is $3. 215. 031. 751 = 151 \cdot 751 \cdot 28351$.
178+ The smallest composite number that fails this test is $3, 215, 031, 751 = 151 \cdot 751 \cdot 28351$.
179179And for testing 64 bit integer it is enough to check the first 12 prime bases: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, and 37.
180180
181181This results in the following deterministic implementation:
182182
183183```cpp
184- bool MillerRabin(u64 n, int iter) {
184+ bool MillerRabin(u64 n, int iter) { // returns true if n is prime, else returns false.
185185 if (n < 2)
186186 return false;
187187
@@ -195,7 +195,7 @@ bool MillerRabin(u64 n, int iter) {
195195 for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
196196 if (n == a)
197197 return true;
198- if (trial_composite (n, a, d, r))
198+ if (check_composite (n, a, d, r))
199199 return false;
200200 }
201201 return true;
0 commit comments