Advertisement
Patresss

Untitled

Sep 2nd, 2014
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int euclid (int a, int b);
  5.  
  6. int main(int argc, char* argv[]) {
  7.  
  8. int a, b;
  9.  
  10. //if (argc != 3) {
  11. //printf("Usage:\n\n\teuclid \n\n"Smilie: ;);
  12. //exit(1);
  13. //}
  14.  
  15.  
  16.  
  17. a = 84;
  18. b = 15;
  19.  
  20. if (a == b)
  21. printf("NWD to: %d\n", a);
  22.  
  23. if (a > b)
  24. printf("NWD to: %d\n", euclid(b, a));
  25. else
  26. printf("NWD to: %d\n", euclid(a, b));
  27.  
  28. return 0;
  29. }
  30.  
  31. int euclid (int a, int b) {
  32.  
  33. static int i = 2;
  34. static int x[10] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0};
  35. static int y[10] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  36.  
  37. printf ("b(%d) \t= (b/a)%d ((a))(%d) \t+ (b%%a)%d \n", b, b/a, a, b%a);
  38. if ((b%a) == 0) {
  39. printf("x = %d \ny = %d\n", x[i-1], y[i-1]);
  40. return a;
  41. }
  42. else {
  43. /*
  44. * For Extended Euclid
  45. */
  46. x[i] = x[i-2] -(b/a) * x[i-1] ;
  47. y[i] = y[i-2]-(b/a) * y[i-1];
  48. printf("%d: x = %d y = %d\n",i, x[i], y[i]);
  49. i++;
  50.  
  51. return (euclid ((b%a) , a));
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement