fork download
  1. #include <stdio.h>
  2.  
  3. int gcd_diff(int a, int b) {
  4. while(a!=b) {
  5. if(a > b) {
  6. a = a - b;
  7. } else{
  8. b = b - a;
  9. }
  10. }
  11. return a;
  12. }
  13.  
  14. int gcd_euclid(int a, int b) {
  15. int r;
  16. while(b) {
  17. r = a % b;
  18. a = b;
  19. b = r;
  20. }
  21. return a;
  22. }
  23.  
  24. int euclid_rec(int a, int b) {
  25. if(b == 0) {
  26. return a;
  27. } else {
  28. return euclid_rec(b, a % b);
  29. }
  30. }
  31.  
  32. int gcd_rec(int a, int b) {
  33. if(a == b) {
  34. return a;
  35. } else {
  36. if(a > b) return gcd_rec(a - b, b);
  37. else
  38. return gcd_rec(a, b -a);
  39. }
  40. }
  41.  
  42. int main() {
  43. int vec[] = {22,4,8,10,1012,72},
  44. n = sizeof(vec)/sizeof(vec[0]),
  45. gcd;
  46.  
  47. gcd = euclid_rec(vec[0],vec[1]);
  48.  
  49. for(int i = 2; i <= n; ++i) {
  50. gcd = euclid_rec(gcd, vec[i]);
  51. }
  52.  
  53. printf("%d", gcd);
  54.  
  55. return(0);
  56. }
Success #stdin #stdout 0s 5604KB
stdin
Standard input is empty
stdout
2