Advertisement
Guest User

Untitled

a guest
May 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<vector>
  4. using namespace std;
  5.  
  6. void sito(bool *tab, unsigned int n)
  7. {
  8. for (int i=2; i*i<=n; i++) //przeszukujemy kolejnych kandydatów na pierwsze
  9. { //wystarczy sprawdziæ do pierwiastka z n
  10. // i<=sqrt(n) - podnosz¹c do kwadratu mamy
  11. // i*i <= n
  12. if(!tab[i]) //jesli liczba jest pierwsza(ma wartosc 0)
  13. for (int j = i*i ; j<=n; j+=i) //to wykreslamy jej wielokrotnosci
  14. tab[j] = 1; //ustawiaj¹c wartosæ na 1
  15. }
  16. }
  17. /*
  18. int x, y;
  19. void euklides(int a, int b)
  20. {
  21. if(b!=0)
  22. {
  23. euklides(b, a%b);
  24.  
  25. int pom = y;
  26. y = x - a/b*y;
  27. x = pom;
  28. }
  29. }
  30. */
  31.  
  32. /*
  33. int x,y;
  34. int r_count =1;
  35. void euklidesAlgorithm(int a , int b)
  36. {
  37. int q, r;
  38.  
  39. r = a % b;
  40.  
  41. if(r!=0 ){
  42. cout<< "r"<<r_count<<" "<< r <<endl;
  43. r_count++;
  44.  
  45. euklidesAlgorithm(b, r);
  46. int q = y;
  47. y = x-a / b*y;
  48. x = q;
  49.  
  50. }*/
  51. /*
  52. int rCounter = 1;
  53. int euklidesAlgorithm(int a, int b, int& x, int& y, bool start){
  54.  
  55. static int staticX[2] = {};
  56. static int staticY[2] = {};
  57. int q, r, r2, q2;
  58.  
  59. r = a % b ;
  60. q = (a - r) / b;
  61. cout<< "r"<<rCounter<<" = "<<a << " - "<< q<< " * "<< b<< " = "<< r<< "\n";
  62. rCounter++;
  63. if( r != 0){
  64. return euklidesAlgorithm(b , r);
  65. }
  66. return b;
  67. }*/
  68.  
  69.  
  70. int euklidesAlgorithm(int a, int b, int& x, int& y, bool start= true)
  71. {
  72. static int staticX[2] = {};
  73. static int staticY[2] = {};
  74.  
  75. int r = a % b;
  76. int q = (a - r) / b;
  77.  
  78. std::cout << a << " = " << q << " * " << b << " + " << r << "\n";
  79.  
  80. if (r != 0) {
  81. // Wyczyść wartości x i y, jeśli jest to pierwsze wywołanie funkcji
  82. if (start) {
  83. staticX[0] = 0;
  84. staticX[1] = 1;
  85. staticY[0] = 1;
  86. staticY[1] = -q;
  87. }
  88. else {
  89. int tempX = staticX[1];
  90. staticX[1] = staticX[0] - staticX[1] * q;
  91. staticX[0] = tempX;
  92.  
  93. int tempY = staticY[1];
  94. staticY[1] = staticY[0] - staticY[1] * q;
  95. staticY[0] = tempY;
  96. }
  97.  
  98. std::cout << "x = " << staticX[1] << " y = " << staticY[1] << "\n\n";
  99.  
  100. return euklidesAlgorithm(b, r, x, y, false);
  101. }
  102.  
  103. x = staticX[1];
  104. y = staticY[1];
  105.  
  106. return b;
  107. }
  108.  
  109.  
  110.  
  111.  
  112. /* cout<< a/b<<endl;
  113. r1 = a%b;
  114. cout<< " r1 = "<< r1;
  115. r2 = b%r1;
  116. cout<< " r2 = "<< r2<<endl;
  117. r3 = r1%r2;
  118. cout<< " r3 = "<< r3<<endl;
  119. q1= (a -r1)/b;
  120. cout<< " q1: " <<q1<<endl;
  121. q2 = (b-r2)/r1;
  122. cout<<"q2 = "<< q2<<endl;
  123. q3 = (r1- r3) / r2<<endl;
  124. cout<< "q3 = "<<q3<<endl;
  125.  
  126.  
  127. }
  128. */
  129.  
  130. int main()
  131.  
  132. {
  133. int x, y;
  134. cout<< " \nWpisz liczbe a i b"<<endl;
  135. int a = 1920;
  136. int b = 162;
  137. int result = euklidesAlgorithm(a,b,x,y );
  138.  
  139. cout<< "NWD ( "<<a<<", "<<b<< " )"<<" = "<< result;
  140.  
  141.  
  142.  
  143.  
  144.  
  145. /*
  146. x = 1, y = 0;
  147. int a, b;
  148.  
  149. cout<<"Podaj liczby: ";
  150. cin>>a>>b;
  151.  
  152. euklides(a, b);
  153.  
  154. cout<<"nwd("<<a<<", "<<b<<") = "
  155. <<a<<" * "<<x<<" + "<<b<<" * "<<y<<" = "
  156. <<a*x+b*y<<endl;
  157. */
  158.  
  159.  
  160. /*
  161. int n;
  162. bool *tab;
  163.  
  164. cout<<"Podaj zakres górny przedzia³u: ";
  165. n=2800000;
  166.  
  167. tab = new bool [n+1];
  168. for(int i=2; i<=n; i++) //zerowanie tablicy
  169. tab[i] = 0;
  170.  
  171. sito(tab, n); //przesianie liczb
  172.  
  173. cout<<"Kolejne liczby pierwsze z przedzia³u [2.."<<n<<"]: ";
  174. int primecounter=0;
  175.  
  176. cout<< "Wpisz liczbe n: "<<endl;
  177. int d;
  178. cin>>d;
  179. unsigned long long int liczba;
  180. for(int i=2;i<=n;i++)
  181. if(!tab[i] && primecounter<d){
  182. // cout<<i<<" ";
  183. liczba=i;
  184. primecounter++;
  185. }
  186.  
  187. cout<< endl;
  188. cout<<"N-ta liczba pierwsza: "<< liczba;
  189.  
  190. delete []tab;*/
  191.  
  192. cin.get();
  193.  
  194. system("pause");
  195. return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement