Skip to content

Commit 5f35dc9

Browse files
committed
Primality tests: Add trivial code for seven-base
1 parent 624c1e1 commit 5f35dc9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/algebra/primality_tests.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,31 @@ bool MillerRabin(u64 n) { // returns true if n is prime, else returns false.
215215
It's also possible to do the check with only 7 bases: 2, 325, 9375, 28178, 450775, 9780504 and 1795265022.
216216
However, since these numbers (except 2) are not prime, you need to check additionally if the number you are checking is equal to any prime divisor of those bases: 2, 3, 5, 13, 19, 73, 193, 407521, 299210837.
217217

218+
```cpp
219+
bool MillerRabin(u64 n) { // returns true if n is prime, else returns false.
220+
if (n < 2)
221+
return false;
222+
223+
int r = 0;
224+
u64 d = n - 1;
225+
while ((d & 1) == 0) {
226+
d >>= 1;
227+
r++;
228+
}
229+
230+
for (int a : {3, 5, 13, 19, 73, 193, 407521, 299210837}) {
231+
if (n == a)
232+
return true;
233+
}
234+
235+
for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
236+
if (check_composite(n, a, d, r))
237+
return false;
238+
}
239+
return true;
240+
}
241+
```
242+
218243
## Practice Problems
219244
220245
- [SPOJ - Prime or Not](https://www.spoj.com/problems/PON/)

0 commit comments

Comments
 (0)