How do computers evaluate the GCD of two integers?

In summary, there are various algorithms that can be used to compute the GCD of two integers, but the most commonly used and efficient one is the Euclidean algorithm. This algorithm involves using repeated subtractions and is relatively cheap compared to other methods. However, there are alternative algorithms that can compute the GCD faster for longer inputs.
  • #1
matqkks
285
5
Do they use the Euclidean Algorithm?
 
Mathematics news on Phys.org
  • #2
Computers do not have minds of their own. A program to evaluate GCD of two integers would be written by a programmer, who would use whatever algorithm he/she feels best.
 
  • Like
Likes berkeman
  • #3
Check the rosettacode.org site someone may have developed the code in whatever language you are interested in.
 
  • #4
Here’s the Euclidean algorithm

https://en.m.wikipedia.org/wiki/Euclidean_algorithm

I imagine math software would use this algorithm to compute it as subtractions are relatively cheap compared to multiplications or divisions.

Another remote possibility is it uses a combination of lookup table and Euclidean algorithm to speed things up although that may take up more memory than necessary and depending on the table size more costly time wise.
 
  • Like
Likes Janosh89 and berkeman
  • #5
jedishrfu said:
I imagine math software would use this algorithm to compute it as subtractions are relatively cheap compared to multiplications or divisions.
You still need some sort of division to calculate how much you have to subtract. Simply subtracting the smaller number from the larger number repeatedly has a really bad worst-case runtime if the numbers are very different (exponential, while divisions make the method scale with the input length squared).

There are alternative algorithms that can compute the GCD faster than quadratic runtime for longer inputs.
 
  • Like
Likes jedishrfu

1. What is the GCD of two integers?

The GCD (Greatest Common Divisor) of two integers is the largest positive integer that divides both numbers without a remainder.

2. How do computers determine the GCD of two integers?

Computers use the Euclidean algorithm to find the GCD of two integers. This algorithm involves repeatedly dividing the larger number by the smaller number until the remainder is zero. The last non-zero remainder is the GCD.

3. Can computers find the GCD of any two integers?

Yes, computers can find the GCD of any two integers, regardless of their size or whether they are positive or negative.

4. How efficient is the GCD algorithm used by computers?

The Euclidean algorithm used by computers to find the GCD is very efficient and has a time complexity of O(log n), where n is the larger of the two input numbers. This means that the time taken to find the GCD increases slowly as the input numbers get larger.

5. Can computers evaluate the GCD of more than two integers?

Yes, computers can evaluate the GCD of more than two integers by finding the GCD of two of the numbers and then finding the GCD of that result with the next number, and so on until all numbers have been evaluated.

Similar threads

Replies
1
Views
1K
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
13
Views
715
Replies
23
Views
1K
  • Precalculus Mathematics Homework Help
Replies
5
Views
841
Replies
5
Views
2K
  • General Math
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
5
Views
1K
  • General Math
Replies
1
Views
1K
  • General Math
Replies
1
Views
1K
Back
Top