Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <bitset>
  4.  
  5. using namespace std;
  6.  
  7. int repeated_squaring_modular_exponentiation(int input, int power, int modulo)
  8. {
  9.     std::bitset<32> bits(power);
  10.     std::vector<int> powers{ input };
  11.     int pw = 2;
  12.     int temp = input;
  13.     while (pw <= power)
  14.     {
  15.         temp = (temp * temp) % modulo;
  16.         powers.push_back(temp);
  17.         pw *= 2;
  18.     }
  19.  
  20.     int result = 1;
  21.     for (unsigned int i = 0; i < powers.size(); ++i)
  22.     {
  23.         if (!bits.test(i))
  24.             continue;
  25.  
  26.         result = (result * powers[i]) % modulo;
  27.     }
  28.  
  29.     return result;
  30. }
  31.  
  32. bool miller_rabin(int n, int s, int t, int b)
  33. {
  34.     // Two possible cases:
  35.     // b^t = 1 mod n
  36.     // or
  37.     // there is a j: 0 <= j < s such that b^((2^j)*t) = -1 mod n
  38.  
  39.     if (repeated_squaring_modular_exponentiation(b, t, n) == 1)
  40.         return true;
  41.  
  42.     for (int j = 0; j < s; j++)
  43.         if (repeated_squaring_modular_exponentiation(b, repeated_squaring_modular_exponentiation(2, j, n) * t, n) == n - 1)
  44.             return true;
  45.  
  46.     return false;
  47. }
  48.  
  49. int gcd(int a, int b) {
  50.     return b == 0 ? a : gcd(b, a % b);
  51. }
  52.  
  53. int main() {
  54.     int p, q, n;
  55.     cout << "Input p: ";
  56.     cin >> p;
  57.     cout << "Input q: ";
  58.     cin >> q;
  59.     n = p * q;
  60.     int max_bases = (p - 1) * (q - 1);
  61.  
  62.     int temp = n - 1;
  63.     int s = 0;
  64.     while (!(temp % 2))
  65.     {
  66.         s++;
  67.         temp /= 2;
  68.     }
  69.  
  70.     cout << "n = " << n << '\n';
  71.     cout << "The equation is:\n";
  72.     cout << n << " - 1 = 2^" << s << " * " << temp << '\n';
  73.     cout << "Number of bases to check: " << max_bases;
  74.  
  75.     cout << "\nn is pseudoprime with respect to the following bases: \n";
  76.     int bases_checked = 0;
  77.     int b = 1;
  78.     string bases = "";
  79.     int bases_found = 0;
  80.     while (bases_checked < max_bases)
  81.     {
  82.         if (gcd(n, b) == 1)
  83.         {
  84.             if (miller_rabin(n, s, temp, b))
  85.             {
  86.                 bases = bases + to_string(b) + ", ";
  87.                 bases_found++;
  88.             }
  89.             bases_checked++;
  90.         }
  91.         b++;
  92.     }
  93.  
  94.     bases.resize(bases.size() - 2);
  95.     cout << bases << "\n";
  96.     cout << "Statistics: " << n << " is pseudoprime to " << bases_found * 100.0 / max_bases << "% of the bases.\n";
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement