summaryrefslogtreecommitdiff
path: root/math/primitiveRoot.cpp
diff options
context:
space:
mode:
authorPaul Jungeblut <paul.jungeblut@gmail.com>2016-10-15 23:30:44 +0200
committerPaul Jungeblut <paul.jungeblut@gmail.com>2016-10-15 23:30:44 +0200
commit463e389ea499dcc26314196c014a85ee9b124847 (patch)
tree64fc77a603df88c4d898fba452c60006194c818a /math/primitiveRoot.cpp
parent53c8e56d8b0ee3b4374ab90630673b971ddf2710 (diff)
Adding primitive root and discrete logarithm.
Diffstat (limited to 'math/primitiveRoot.cpp')
-rw-r--r--math/primitiveRoot.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/math/primitiveRoot.cpp b/math/primitiveRoot.cpp
new file mode 100644
index 0000000..3ad828d
--- /dev/null
+++ b/math/primitiveRoot.cpp
@@ -0,0 +1,23 @@
+// Ist g Primitivwurzel modulo p. Teste zufällige g, um eine zu finden.
+bool is_primitive(ll g, ll p) {
+ map<ll, int> facs;
+ factor(p - 1, facs);
+ for (auto &f : facs)
+ if (1 == powMod(g, (p - 1) / f.first, p)) return false;
+ return true;
+}
+
+// Alternativ: Generator zum Finden. -1 falls keine existiert.
+ll generator (ll p) { // Laufzeit: O(ans*log(phi(n))*log(n))
+ map<ll, int> facs;
+ factor(n, facs);
+ ll phi = phi(p), n = phi;
+
+ for (ll res = 2; res <= p; res++) {
+ bool ok = true;
+ for (auto &f : facs)
+ ok &= powMod(res, phi / f.first, p) != 1;
+ if (ok) return res;
+ }
+ return -1;
+}