summaryrefslogtreecommitdiff
path: root/math/rho.cpp
diff options
context:
space:
mode:
authormzuenni <michi.zuendorf@gmail.com>2022-06-27 17:19:28 +0200
committermzuenni <michi.zuendorf@gmail.com>2022-06-27 17:19:28 +0200
commit5ab8a5088b729a9953b8dff1b2a985dc8fb2098b (patch)
treeed40d6936c0e9eee40ba62751cbf99ecddbaddc2 /math/rho.cpp
parentadabbad9c51cf7cd3874bfde8eac1fbcf84fec10 (diff)
updated tcr
Diffstat (limited to 'math/rho.cpp')
-rw-r--r--math/rho.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/math/rho.cpp b/math/rho.cpp
new file mode 100644
index 0000000..bd30902
--- /dev/null
+++ b/math/rho.cpp
@@ -0,0 +1,22 @@
+ll rho(ll n) { // Findet Faktor < n, nicht unbedingt prim.
+ if (n % 2 == 0) return 2;
+ ll c = rand() % n, x = rand() % n, y = x, d = 1;
+ // mulmod or int128
+ auto f = [&](ll x){return ((x * x) % n + c) % n;};
+ while (d == 1) {
+ x = f(x); y = f(f(y));
+ d = gcd(abs(x - y), n);
+ }
+ return d == n ? rho(n) : d;
+}
+
+void factor(ll n, map<ll, int>& facts) {
+ if (n == 1) return;
+ if (isPrime(n)) {
+ facts[n]++;
+ return;
+ }
+ ll f = rho(n);
+ factor(n / f, facts);
+ factor(f, facts);
+}