Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. namespace Laba
  5. {
  6.  
  7.  
  8. class MyRandom
  9. {
  10. static Random rand = new Random();
  11. public static BigInteger gen(BigInteger low, BigInteger up) // генерация числа в диапазоне [low,up)
  12. {
  13. int sz = (int)BigInteger.Log(up, 2);
  14. BigInteger ans = 0, p = 1;
  15. do
  16. {
  17. ans = 0;
  18. p = 1;
  19. for (int i = 0; i < sz; i++)
  20. {
  21. ans += rand.Next(2) * p;
  22. p *= 2;
  23. }
  24. }
  25. while (ans < low || ans >= up);
  26.  
  27. return ans;
  28. }
  29. }
  30.  
  31.  
  32. class PrimeTest
  33. {
  34. static int Jac(BigInteger a, BigInteger n)
  35. {
  36. int ans = 1;
  37.  
  38.  
  39. //избавление от четности
  40. while (a > 1)
  41. {
  42. int x = 0;
  43. while (a % 2 == 0)
  44. {
  45. x++;
  46. a /= 2;
  47. }
  48. if (x % 2 == 1 && (n % 8 == 3 || n % 8 == 5))
  49. ans = -ans;
  50. //квадратичный закон взаимности
  51. if (a % 4 == 3 && n % 4 == 3)
  52. ans = -ans;
  53.  
  54. BigInteger temp = a;
  55. a = n % temp;
  56. n = temp;
  57. }
  58. return ans;
  59. }
  60.  
  61. public static bool is_prime(BigInteger n)
  62. {
  63. if (n < 2 || n%2==0)
  64. return false;
  65. if (n == 2 || n == 3)
  66. return true;
  67.  
  68. int k = (int)BigInteger.Log(n, 2);
  69. for (int i = 0; i < k; i++)
  70. {
  71. BigInteger a = MyRandom.gen(2, n);
  72. if (BigInteger.GreatestCommonDivisor(a, n) > 1)
  73. return false;
  74.  
  75. BigInteger Jacoby = Jac(a, n);
  76. if (Jacoby == -1)
  77. Jacoby += n;
  78. if (BigInteger.ModPow(a, (n - 1) / 2, n) != Jacoby)
  79. return false;
  80. }
  81. return true;
  82.  
  83. }
  84. }
  85.  
  86.  
  87. class Blum
  88. {
  89.  
  90. public BigInteger P, Q, x0, M;
  91.  
  92. public Blum(BigInteger p, BigInteger q, BigInteger x)
  93. {
  94. P = p;
  95. Q = q;
  96. x0 = x;
  97. M = p * q;
  98. }
  99.  
  100. BigInteger lcm(BigInteger x, BigInteger y)
  101. {
  102. return x / BigInteger.GreatestCommonDivisor(x, y) * y;
  103. }
  104. public BigInteger[] GenerateSequence(int len)
  105. {
  106. BigInteger[] x = new BigInteger[len];
  107. x[0] = x0;
  108. for (int i = 1; i < len; i++)
  109. x[i] = x[i - 1] * x[i - 1] % M;
  110. return x;
  111. }
  112. public BigInteger GenerateIthElement(int i)
  113. {
  114. BigInteger l = lcm(P - 1, Q - 1);
  115.  
  116. BigInteger pow = BigInteger.ModPow(2, i, l);
  117. BigInteger ans = BigInteger.ModPow(x0, pow, M);
  118. return ans;
  119. }
  120. }
  121.  
  122.  
  123.  
  124.  
  125. class MainClass
  126. {
  127.  
  128.  
  129. public static void Main(string[] args)
  130. {
  131. // это блум блум
  132. BigInteger p = 19;
  133. BigInteger q = 11;
  134. BigInteger x0 = 3;
  135. Blum bb = new Blum(p, q, x0);
  136. Console.WriteLine("p = {0}", bb.P);
  137. Console.WriteLine("q = {0}", bb.Q);
  138. Console.WriteLine("M = {0}", bb.M);
  139. Console.WriteLine("x0 = {0}", bb.x0);
  140. int i = 7;
  141. Console.WriteLine("{0} element of sequence is {1}", i, bb.GenerateIthElement(i));
  142.  
  143. BigInteger[] x = bb.GenerateSequence(10);
  144. Console.WriteLine("Sequence:");
  145. for (int k = 0; k < 10; k++)
  146. Console.WriteLine(x[k]);
  147.  
  148. // это Соловей
  149.  
  150. Console.WriteLine();
  151. BigInteger n = 167; // число которое хотим проверить на простоту
  152. if (PrimeTest.is_prime(n))
  153. {
  154. Console.WriteLine("{0} простое", n);
  155. }
  156. else
  157. {
  158. Console.WriteLine("{0} состановное", n);
  159. }
  160. n = 147; // число которое хотим проверить на простоту
  161. if (PrimeTest.is_prime(n))
  162. {
  163. Console.WriteLine("{0} простое", n);
  164. }
  165. else
  166. {
  167. Console.WriteLine("{0} состановное", n);
  168. }
  169. Console.ReadKey();
  170. }
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement