MyBigInt generatePrime() { stringstream ss; MyBigInt tempo("0"); srand(static_cast<unsigned int>(time(NULL))); // genering num_of_bits bits. ss << 1; for (int i = 0; i < num_of_bits - 1; i++) { int p = rand() % 2; if (p) { ss << i; tempo.set_value(tempo + (static_cast<MyBigInt>("2") ^ ss.str())); ss.str(""); } } // Searching for a prime number MyBigInt t(tempo); while (true) { if (Miller_Rabin(t) == "0") { tempo.set_value(tempo + static_cast<MyBigInt>("1")); cout << "Generating" << endl; t.set_value(tempo); } else { return tempo; } } return tempo; }
int main(int argc, char const *argv[]) { int T; LL n; scanf("%d",&T); while(T--) { scanf("%I64d",&n); if(Miller_Rabin(n,20)==0) { printf("Prime\n"); } else { tol=0; findfac(n); LL ans = factor[0]; //printf("tol=%d\n",tol); //for(int i = 0 ; i < tol ; i++) // printf("%lld", factor[i]); //printf("\n"); for(int i=1;i<tol;i++) if(factor[i]<ans) ans=factor[i]; printf("%I64d\n",ans); } } return 0; }
//对n进行素因子分解 void findfac(long long n){ if(Miller_Rabin(n)){//素数 factor[tol++]=n; return; } long long p=n; while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1); findfac(p); findfac(n/p); }
/* *大整数质因数分解 *ret存储n的所有质因子,c是任意一个数字 */ void factorize(ll n, int c, vector<ll> &ret) { if (n == 1) return ; if (Miller_Rabin(n)) { ret.push_back(n); return ; } ll p = n; while (p >= n) p = Pollard_rho(p, c--); factorize(p, c, ret); factorize(n/p, c, ret); }
//对n进行素因子分解 void findfac(LL n) { if(Miller_Rabin(n,20)==0)//素数 { factor[tol++]=n; return; } LL p=n; while(p>=n) p=Pollard_rho(p,rand()%(p-1)+1); findfac(p); findfac(n/p); }
int main(void) { char answer[256]; long t; verylong zn = 0; do { do { printf("enter the security parameter ( >= 1): "); scanf("%d", &t); } while (t < 1); printf("enter the number to be tested below:\n"); zread(&zn); if (Miller_Rabin(t, zn)) printf("number is probably prime\n"); else printf("number is composite\n"); printf("another number (n or y)? "); scanf("%s", answer); } while (tolower(answer[0]) == 'y'); zfree(&zn); return 0; }
// // ГОСТ 30.310-95 - генерація простих чисел. // Всі позначення змінних і пунктів взяті безпосередньо із ГОСТа. // void Gost() { srand(static_cast<unsigned int>(time(NULL))); stringstream ss; MyBigInt N("0"); //пункт 9: MyBigInt Ps("2"); int s = 0, i = 0; double t = num_of_bits; // пункт 1: //unsigned int y0 = rand(); unsigned int y0 = 24265; //int c = rand(); int c = 7341; if (c % 2 == 0) c += 1; // пункт 2: while (t >= 17) { t /= 2; i++; } s = i; s--; // пункт 3: Ps - поідеї найменше число n-bit ss << static_cast<int>(t - 1); Ps.set_value(Ps^ss.str()); ss.str(""); MyBigInt tempo(Ps); while (true) { if (Miller_Rabin(tempo) == "0") { Ps.set_value(Ps + static_cast<MyBigInt>("1")); tempo.set_value(Ps); } else { system("cls"); break; } } // пункт 4: int m = s; // пункт 5: Jump5: double rm = t / 16; // пункт 6 - 7 - 8: Jump6: ss << y0; MyBigInt Ym(ss.str()); ss.str(""); for (int i = 0; i < rm; i++) { y0 = (19381 * y0 + c) % 65536; ss << y0; Ym.set_value(Ym + static_cast<MyBigInt>(ss.str())); ss.str(""); } ss << y0; Ym.set_value(Ym - static_cast<MyBigInt>(ss.str())); //Ym.set_value(Ym * (static_cast<MyBigInt>("2") ^ "161")); ss.str(""); // пункт 9: stringstream ko; ko << (2 * t) - 1; N.set_value((static_cast<MyBigInt>("2") ^ ko.str())); ss.str(""); ss << 16 << rm; N.set_value(N + ( ((static_cast<MyBigInt>("2") ^ ko.str()) * Ym) )); if (N % static_cast<MyBigInt>("2") == "0") N.set_value(N + "1"); ss.str(""); ko.str(""); // пункт 10: int k = 0; ss << k; // пункт 11: Jump11: MyBigInt Pm((N + ss.str())); ss.str(""); // пункт 12: ss << 2 * t; cout << Pm.get_value() << endl; if (Pm > (static_cast<MyBigInt>("2") ^ ss.str())) { ss.str(""); goto Jump6; } ss.str(""); // пункт 13: ss << k; cout << static_cast<MyBigInt>("2").Gorner((Ps * (N + ss.str())), Pm).get_value() << " " << static_cast<MyBigInt>("2").Gorner((N + ss.str()), Pm).get_value() << " " << Pm.get_value()<< endl; if (static_cast<MyBigInt>("2").Gorner((Ps * (N + ss.str())), Pm) == "1" && static_cast<MyBigInt>("2").Gorner((N + ss.str()), Pm) != "1") { system("pause"); m = m - 1; t = t * 2; // пункт 14: if (m >= 0) { goto Jump5; } else { system("cls"); cout << "Victory bitch!" << endl; system("pause"); } } else { k = k + 2; goto Jump11; } }