fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. int gcd_iterations(int a, int b, int iters){
  7. if (b > a){
  8. return gcd_iterations(b, a, iters+1);
  9. }
  10. int r = a % b;
  11. if (r == 0){
  12. return iters;
  13. } else {
  14. return gcd_iterations(b, r, iters + 1);
  15. }
  16. }
  17.  
  18. int main() {
  19. int loops = 30000;
  20. int i, sum, a, b;
  21.  
  22. sum = 0;
  23. for (i = 0; i < loops; i++)
  24. {
  25. a = rand();
  26. b = rand();
  27. sum += gcd_iterations(a, b, 1);
  28. }
  29. float avg;
  30. avg = (float) sum / (float) loops;
  31. cout << avg << endl;
  32. cout << "sample inputs: " << a << " and " << b << endl;
  33. cout << "Bits of an int " << sizeof(int) * 8 << endl;
  34. a = 1836311903;
  35. b = 1134903170;
  36. cout << "Iterations at F44, F43 " << gcd_iterations(a, b, 1) << endl;
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 3296KB
stdin
Standard input is empty
stdout
18.1799
sample inputs: 1057513700 and 1721671889
Bits of an int 32
Iterations at F44, F43 44