OSDN Git Service

fix all prime check fns (#1021)
authorVoile <voileexperiments@gmail.com>
Tue, 5 Mar 2019 03:00:46 +0000 (11:00 +0800)
committerXeonacid <h.dwwwwww@gmail.com>
Tue, 5 Mar 2019 03:00:46 +0000 (11:00 +0800)
`n < 2`全部返回1是甚麼鬼

docs/math/prime.md

index 7fd9e77..02a76eb 100644 (file)
@@ -14,6 +14,7 @@
 
 ```c++
 bool isPrime(a) {
+  if (a < 2) return 0;
   for (int i = 2; i < a; ++i)
     if (a % i == 0) return 0;
   return 1;
@@ -30,6 +31,7 @@ bool isPrime(a) {
 
 ```c++
 bool isPrime(a) {
+  if (a < 2) return 0;
   for (int i = 2; i * i <= a; ++i)
     if (a % i) return 0;
   return 1;
@@ -48,6 +50,7 @@ Miller-Rabin 素性测试(Miller–Rabin primality test)是进阶的素数
 
 ```c++
 bool millerRabin(int n) {
+  if (n < 3) return n == 2;
   for (int i = 1; i <= s; ++i) {
     int a = rand() % (n - 2) + 2;
     if (quickPow(a, n - 1, n) != 1) return 0;
@@ -84,6 +87,7 @@ bool millerRabin(int n) {
 
 ```c++
 bool millerRabbin(int n) {
+  if (n < 3) return n == 2;
   int a = n - 1, b = 0;
   while (a % 2 == 0) a /= 2, ++b;
   for (int i = 1, j; i <= s; ++i) {