Skip to content
This repository was archived by the owner on Nov 29, 2020. It is now read-only.

Commit 561c1bb

Browse files
committed
MillerRabin.cpp
1 parent 0e58b67 commit 561c1bb

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

MillerRabin.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// algorithm - some algorithms in "Introduction to Algorithms", third edition
3+
// Copyright (C) 2018 lxylxy123456
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as
7+
// published by the Free Software Foundation, either version 3 of the
8+
// License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
19+
#ifndef MAIN
20+
#define MAIN
21+
#define MAIN_MillerRabin
22+
#endif
23+
24+
#ifndef FUNC_MillerRabin
25+
#define FUNC_MillerRabin
26+
27+
#include "utils.h"
28+
29+
#include "ModularExponentiation.cpp"
30+
31+
template <typename T>
32+
bool Witness(T a, T n) {
33+
T t = 0, u = n - 1;
34+
while (!(u & 1)) {
35+
t++;
36+
u >>= 1;
37+
}
38+
T x0 = ModularExponentiation(a, u, n);
39+
for (T i = 0; i < t; i++) {
40+
T x1 = x0 * x0 % n;
41+
if (x1 == 1 && x0 != 1 && x0 != n - 1)
42+
return true;
43+
x0 = x1;
44+
}
45+
if (x0 != 1)
46+
return true;
47+
return false;
48+
}
49+
50+
template <typename T>
51+
bool MillerRabin(T n, size_t s) {
52+
std::vector<T> a;
53+
random_integers<T>(a, 1, n - 1, s);
54+
for (auto i = a.begin(); i != a.end(); i++) {
55+
if (Witness(*i, n))
56+
return false;
57+
}
58+
return true;
59+
}
60+
#endif
61+
62+
#ifdef MAIN_MillerRabin
63+
int main(int argc, char *argv[]) {
64+
const size_t nn = get_argv(argc, argv, 1, 30);
65+
const size_t s = get_argv(argc, argv, 2, 5);
66+
const size_t tries = get_argv(argc, argv, 3, 1);
67+
using T = long long int;
68+
std::random_device rd;
69+
std::uniform_int_distribution<T> dis(1 << (nn - 1), 1 << nn);
70+
for (size_t i = 0; i < tries; i++) {
71+
T n = get_argv(argc, argv, 4 + i, dis(rd) << 1 | 1);
72+
bool ans = MillerRabin(n, s);
73+
std::cout << n << "\t" << std::boolalpha << ans << std::endl;
74+
}
75+
return 0;
76+
}
77+
#endif
78+

Pseudoprime.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// algorithm - some algorithms in "Introduction to Algorithms", third edition
3+
// Copyright (C) 2018 lxylxy123456
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as
7+
// published by the Free Software Foundation, either version 3 of the
8+
// License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
19+
#ifndef MAIN
20+
#define MAIN
21+
#define MAIN_Pseudoprime
22+
#endif
23+
24+
#ifndef FUNC_Pseudoprime
25+
#define FUNC_Pseudoprime
26+
27+
#include "utils.h"
28+
29+
#include "ModularExponentiation.cpp"
30+
31+
template <typename T>
32+
bool Pseudoprime(T n) {
33+
if (ModularExponentiation((T) 2, n - 1, n) != 1)
34+
return false;
35+
else
36+
return true;
37+
}
38+
#endif
39+
40+
#ifdef MAIN_Pseudoprime
41+
int main(int argc, char *argv[]) {
42+
const size_t nn = get_argv(argc, argv, 1, 30);
43+
const size_t tries = get_argv(argc, argv, 2, 1);
44+
using T = long long int;
45+
std::random_device rd;
46+
std::uniform_int_distribution<T> dis(1 << (nn - 1), 1 << nn);
47+
for (size_t i = 0; i < tries; i++) {
48+
T n = get_argv(argc, argv, 3 + i, dis(rd) << 1 | 1);
49+
std::cout << n << "\t" << std::boolalpha << Pseudoprime(n) << std::endl;
50+
}
51+
return 0;
52+
}
53+
#endif
54+

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@
201201
| 31 | Euclid.cpp | Extended Euclid | 937 |
202202
| 31 | ModLinEquationSolver.cpp | Modular Linear Equation Solver | 949 |
203203
| 31 | ModularExponentiation.cpp | Modular Exponentiation | 957 |
204+
| 31 | Pseudoprime.cpp | Pseudoprime | 967 |
205+
| 31 | MillerRabin.cpp | Witness | 969 |
206+
| 31 | MillerRabin.cpp | Miller Rabin | 970 |
204207

205208
# Supplementary Files
206209
* `utils.h`: Utils

0 commit comments

Comments
 (0)