#include <iostream>
#include <cstdlib>
using namespace std;
int gcd_iterations(int a, int b, int iters){
if (b > a){
return gcd_iterations(b, a, iters+1);
}
int r = a % b;
if (r == 0){
return iters;
} else {
return gcd_iterations(b, r, iters + 1);
}
}
int main() {
int loops = 30000;
int i, sum, a, b;
sum = 0;
for (i = 0; i < loops; i++)
{
a = rand();
b = rand();
sum += gcd_iterations(a, b, 1);
}
float avg;
avg = (float) sum / (float) loops;
cout << avg << endl;
cout << "sample inputs: " << a << " and " << b << endl;
cout << "Bits of an int " << sizeof(int) * 8 << endl;
a = 1836311903;
b = 1134903170;
cout << "Iterations at F44, F43 " << gcd_iterations(a, b, 1) << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGxpYj4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgZ2NkX2l0ZXJhdGlvbnMoaW50IGEsIGludCBiLCBpbnQgaXRlcnMpewoJaWYgKGIgPiBhKXsKCQlyZXR1cm4gZ2NkX2l0ZXJhdGlvbnMoYiwgYSwgaXRlcnMrMSk7Cgl9CglpbnQgciA9IGEgJSBiOwoJaWYgKHIgPT0gMCl7CgkJcmV0dXJuIGl0ZXJzOwoJfSBlbHNlIHsKCQlyZXR1cm4gZ2NkX2l0ZXJhdGlvbnMoYiwgciwgaXRlcnMgKyAxKTsKCX0KfQoKaW50IG1haW4oKSB7CiAgICBpbnQgbG9vcHMgPSAzMDAwMDsKICAgIGludCBpLCBzdW0sIGEsIGI7CiAgICAKICAgIHN1bSA9IDA7CiAgICBmb3IgKGkgPSAwOyBpIDwgbG9vcHM7IGkrKykKICAgIHsKICAgIAlhID0gcmFuZCgpOwogICAgCWIgPSByYW5kKCk7CiAgICAJc3VtICs9IGdjZF9pdGVyYXRpb25zKGEsIGIsIDEpOwogICAgfQogICAgZmxvYXQgYXZnOwogICAgYXZnID0gKGZsb2F0KSBzdW0gLyAoZmxvYXQpIGxvb3BzOwoJY291dCA8PCBhdmcg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