Hello! I have found this GCD (Greatest Common Divisor) algorithm and wonder if someone knows an even more efficient one? The faster the better. :cool:
P.S. I indent with a tab length of 4 spaces in my environment, but here a tab is 8 spaces :mad: . Can I fix this with some setting? Would be nice. Thanks!!Code:int gcd( int a, int b ) {
int t;
if ( a < 0 )
a = -a;
if ( ! b )
return a;
if ( b < 0 )
b = -b;
if ( ! a )
return b;
t = 0;
while ( ! ( ( a | b ) & 1 ) ) {
a >>= 1;
b >>= 1;
++t;
}
while ( ! ( a & 1 ) )
a >>= 1;
while ( ! ( b & 1 ) )
b >>= 1;
while ( a != b ) {
if ( a > b ) {
a -= b;
do {
a >>= 1;
} while ( ! ( a & 1 ) );
}
else {
b -= a;
do {
b >>= 1;
} while ( ! ( b & 1 ) );
}
}
return a << t;
}