Created
May 1, 2016 07:03
-
-
Save ikr7/1a3bde63b11b2bb564a3d5605798606a to your computer and use it in GitHub Desktop.
素因数分解コンソール
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <gmpxx.h> | |
using namespace std; | |
mpz_class GCD (mpz_class m, mpz_class n) { | |
mpz_class t; | |
while (n != 0) { | |
t = m; | |
m = n; | |
n = t % n; | |
} | |
return m; | |
} | |
mpz_class f (mpz_class s, mpz_class n) { | |
srand(s.get_ui()); | |
return rand() % n; | |
} | |
mpz_class factorize (mpz_class n) { | |
mpz_class x = 2; | |
mpz_class y = 2; | |
mpz_class d = 1; | |
while (d == 1) { | |
x = f(x, n); | |
y = f(f(y, n), n); | |
d = GCD(abs(x - y), n); | |
} | |
if (d == n) { | |
return 0; | |
} | |
return d; | |
} | |
int main() { | |
mpz_class a; | |
string buf; | |
cout << "Factorize(n) >> "; | |
while(cin >> buf) { | |
a.set_str(buf, 10); | |
cout << factorize(a).get_str() << endl; | |
cout << "Factorize(n) >> "; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment