Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<vector>
  4. #include<math.h>
  5.  
  6. using namespace std;
  7.  
  8. void sito(bool *tab, unsigned int n)
  9. {
  10. for (int i=2; i*i<=n; i++) //przeszukujemy kolejnych kandydatów na pierwsze
  11. { //wystarczy sprawdziæ do pierwiastka z n
  12. // i<=sqrt(n) - podnosz¹c do kwadratu mamy
  13. // i*i <= n
  14. if(!tab[i]) //jesli liczba jest pierwsza(ma wartosc 0)
  15. for (int j = i*i ; j<=n; j+=i) //to wykreslamy jej wielokrotnosci
  16. tab[j] = 1; //ustawiaj¹c wartosæ na 1
  17. }
  18. }
  19. /*
  20. int x, y;
  21. void euklides(int a, int b)
  22. {
  23. if(b!=0)
  24. {
  25. euklides(b, a%b);
  26.  
  27. int pom = y;
  28. y = x - a/b*y;
  29. x = pom;
  30. }
  31. }
  32. */
  33.  
  34. /*
  35. int x,y;
  36. int r_count =1;
  37. void euklidesAlgorithm(int a , int b)
  38. {
  39. int q, r;
  40.  
  41. r = a % b;
  42.  
  43. if(r!=0 ){
  44. cout<< "r"<<r_count<<" "<< r <<endl;
  45. r_count++;
  46.  
  47. euklidesAlgorithm(b, r);
  48. int q = y;
  49. y = x-a / b*y;
  50. x = q;
  51.  
  52. }*/
  53. /*
  54. int rCounter = 1;
  55. int euklidesAlgorithm(int a, int b, int& x, int& y, bool start){
  56.  
  57. static int staticX[2] = {};
  58. static int staticY[2] = {};
  59. int q, r, r2, q2;
  60.  
  61. r = a % b ;
  62. q = (a - r) / b;
  63. cout<< "r"<<rCounter<<" = "<<a << " - "<< q<< " * "<< b<< " = "<< r<< "\n";
  64. rCounter++;
  65. if( r != 0){
  66. return euklidesAlgorithm(b , r);
  67. }
  68. return b;
  69. }*/
  70.  
  71.  
  72. int euklidesAlgorithm(int a, int b, int& x, int& y, bool start= true)
  73. {
  74. static int staticX[2] = {};
  75. static int staticY[2] = {};
  76.  
  77. int r = a % b;
  78. int q = (a - r) / b;
  79.  
  80. std::cout << a << " = " << q << " * " << b << " + " << r << "\n";
  81.  
  82. if (r != 0) {
  83. // Wyczyść wartości x i y, jeśli jest to pierwsze wywołanie funkcji
  84. if (start) {
  85. staticX[0] = 0;
  86. staticX[1] = 1;
  87. staticY[0] = 1;
  88. staticY[1] = -q;
  89. }
  90. else {
  91. int tempX = staticX[1];
  92. staticX[1] = staticX[0] - staticX[1] * q;
  93. staticX[0] = tempX;
  94.  
  95. int tempY = staticY[1];
  96. staticY[1] = staticY[0] - staticY[1] * q;
  97. staticY[0] = tempY;
  98. }
  99.  
  100. std::cout << "x = " << staticX[1] << " y = " << staticY[1] << "\n\n";
  101.  
  102. return euklidesAlgorithm(b, r, x, y, false);
  103. }
  104.  
  105. x = staticX[1];
  106. y = staticY[1];
  107.  
  108. return b;
  109. }
  110.  
  111.  
  112.  
  113.  
  114. /* cout<< a/b<<endl;
  115. r1 = a%b;
  116. cout<< " r1 = "<< r1;
  117. r2 = b%r1;
  118. cout<< " r2 = "<< r2<<endl;
  119. r3 = r1%r2;
  120. cout<< " r3 = "<< r3<<endl;
  121. q1= (a -r1)/b;
  122. cout<< " q1: " <<q1<<endl;
  123. q2 = (b-r2)/r1;
  124. cout<<"q2 = "<< q2<<endl;
  125. q3 = (r1- r3) / r2<<endl;
  126. cout<< "q3 = "<<q3<<endl;
  127.  
  128.  
  129. }
  130. */
  131.  
  132. int decToBinSize(int dec)
  133. {
  134. for (int i = 0; ; i++) {
  135. if (pow(2, i) > dec) {
  136. return i;
  137. }
  138. }
  139. }
  140.  
  141. std::vector<bool> decToBin(int dec)
  142. {
  143. int size = decToBinSize(dec);
  144. std::vector<bool> bin;
  145.  
  146. for (int i = size - 1; i >= 0; i--) {
  147. if (pow(2, i) <= dec) {
  148. dec -= pow(2, i);
  149. bin.push_back(1);
  150. }
  151. else {
  152. bin.push_back(0);
  153. }
  154. }
  155.  
  156. return bin;
  157. }
  158.  
  159. unsigned long long int modulo(unsigned long long int a, const unsigned long long int b, unsigned long long int c)
  160. {
  161. std::vector<bool> binB = decToBin(b);
  162. int binSize = decToBinSize(b);
  163.  
  164. unsigned int currentNumber = a; // pow(a, 1)
  165. currentNumber %= c;
  166.  
  167. unsigned long long int result = 1;
  168.  
  169. if (binB[binSize - 1] == 1) { // binb to potega zapisana w formie binarnej
  170. result *= currentNumber;
  171. result %= c;
  172. }
  173.  
  174. for (int i = 1; i < binSize; i++) {
  175. currentNumber = pow(currentNumber, 2);
  176. currentNumber %= c;
  177.  
  178. if (binB[binSize - 1 - i] == 1) {
  179. result *= currentNumber;
  180. result %= c;
  181. }
  182. }
  183.  
  184. return result;
  185. }
  186.  
  187.  
  188. int main()
  189.  
  190. {
  191. //int x, y;
  192. //cout<< " \nWpisz liczbe a i b"<<endl;
  193. //int a = 1920;
  194. //int b = 162;
  195. //int result = euklidesAlgorithm(a,b,x,y );
  196.  
  197. //cout<< "NWD ( "<<a<<", "<<b<< " )"<<" = "<< result;
  198.  
  199. cout << modulo(30, 2, 42) << endl;
  200.  
  201. int finalNumber;
  202. bool *tab;
  203.  
  204. cout<<"Podaj zakres górny przedzia³u: ";
  205. finalNumber=2800000;
  206.  
  207. tab = new bool [finalNumber+1];
  208. for(int i=2; i<=finalNumber; i++) //zerowanie tablicy
  209. tab[i] = 0;
  210.  
  211. sito(tab, finalNumber); //przesianie liczb
  212.  
  213. cout<<"Kolejne liczby pierwsze z przedzia³u [2.."<<finalNumber<<"]: ";
  214. int primecounter=0;
  215.  
  216. int p = 0;
  217. int q = 0;
  218. cout<< "Wpisz liczbe 1: "<<endl;
  219. int n1;
  220. cin>>n1;
  221. cout<< "Wpisz liczbe 2: "<<endl;
  222. int n2;
  223. cin>>n2;
  224. unsigned long long int liczba;
  225. for(int i=2;i<=finalNumber;i++)
  226. if(!tab[i] && (p == 0 || q == 0)){
  227. // cout<<i<<" ";
  228. liczba=i;
  229. primecounter++;
  230. if (primecounter == n1)
  231. p = liczba;
  232. if (primecounter == n2)
  233. q = liczba;
  234. }
  235.  
  236. int n = p*q;
  237. cout << "n=" << n << endl;
  238.  
  239. int m = (p-1)*(q-1);
  240. cout << "m=" << m << endl;
  241.  
  242. int e,x,y;
  243. do
  244. {
  245. cout<< "Podaj losowa liczbe z przedzialu <1;" << m << ">:";
  246. cin>>e;
  247. } while(e < 1 || e > m || euklidesAlgorithm(e, m, x, y) != 1);
  248.  
  249. cout << "x=" << x << endl;
  250.  
  251. int d, s, t;
  252.  
  253. cout<< "Podaj t: ";
  254. cin>>t;
  255.  
  256. if (x > 0)
  257. {
  258. d = x;
  259. cout << "d = " << d << endl;
  260. }
  261.  
  262. cout << "t " << t << " e " << e << " n " << n << endl;
  263. s = modulo(t, e, n);
  264. cout << "s = " << s << endl;
  265.  
  266. t = modulo(s, d, n);
  267. cout << "t = " << t << endl;
  268.  
  269. //cout<< endl;
  270. //cout<<"N-ta liczba pierwsza: "<< liczba;
  271.  
  272. delete []tab;
  273.  
  274.  
  275. /*
  276. x = 1, y = 0;
  277. int a, b;
  278.  
  279. cout<<"Podaj liczby: ";
  280. cin>>a>>b;
  281.  
  282. euklides(a, b);
  283.  
  284. cout<<"nwd("<<a<<", "<<b<<") = "
  285. <<a<<" * "<<x<<" + "<<b<<" * "<<y<<" = "
  286. <<a*x+b*y<<endl;
  287. */
  288.  
  289.  
  290. /*
  291. int n;
  292. bool *tab;
  293.  
  294. cout<<"Podaj zakres górny przedzia³u: ";
  295. n=2800000;
  296.  
  297. tab = new bool [n+1];
  298. for(int i=2; i<=n; i++) //zerowanie tablicy
  299. tab[i] = 0;
  300.  
  301. sito(tab, n); //przesianie liczb
  302.  
  303. cout<<"Kolejne liczby pierwsze z przedzia³u [2.."<<n<<"]: ";
  304. int primecounter=0;
  305.  
  306. cout<< "Wpisz liczbe n: "<<endl;
  307. int d;
  308. cin>>d;
  309. unsigned long long int liczba;
  310. for(int i=2;i<=n;i++)
  311. if(!tab[i] && primecounter<d){
  312. // cout<<i<<" ";
  313. liczba=i;
  314. primecounter++;
  315. }
  316.  
  317. cout<< endl;
  318. cout<<"N-ta liczba pierwsza: "<< liczba;
  319.  
  320. delete []tab;*/
  321.  
  322. cin.get();
  323.  
  324. system("pause");
  325. return 0;
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement