One way is to calculate their gcd and check if it is 1.
Is there some faster way?
Co-prime numbers or relatively prime numbers are those numbers that have their HCF (Highest Common Factor) as 1. In other words, two numbers are co-prime if they no common factor other than 1.
Two integers are relatively prime or Coprime when there are no common factors other than 1. This means that no other integer could divide both numbers evenly.
if you're running on a machine for which division/remainder is significantly more expensive than shifts, consider binary GCD.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With