What is the fastest way to check if two given numbers are coprime?

Lazer picture Lazer · Sep 27, 2009 · Viewed 12.8k times · Source

One way is to calculate their gcd and check if it is 1.

Is there some faster way?

Answer

jason picture jason · Sep 27, 2009

The Euclidean algorithm (computes gcd) is very fast. When two numbers are drawn uniformly at random from [1, n], the average number of steps to compute their gcd is O(log n). The average computation time required for each step is quadratic in the number of digits.

There are alternatives that perform somewhat better (i.e., each step is subquadratic in the number of digits), but they are only effective on very large integers. See, for example, On Schönhage's algorithm and subquadratic integer gcd computation.