How do computers evaluate the GCD of two integers?

Click For Summary

Discussion Overview

The discussion centers on how computers evaluate the greatest common divisor (GCD) of two integers, exploring various algorithms and programming considerations. The scope includes theoretical algorithms, practical programming approaches, and performance implications.

Discussion Character

  • Exploratory, Technical explanation, Debate/contested

Main Points Raised

  • Some participants propose that the Euclidean Algorithm is commonly used for computing the GCD.
  • Others argue that the choice of algorithm depends on the programmer's preferences and the specific requirements of the program.
  • A suggestion is made to refer to rosettacode.org for code examples in different programming languages.
  • One participant mentions that while the Euclidean algorithm is efficient due to the relative cheapness of subtractions, it may still require divisions, which could affect performance.
  • Another point raised is that alternative algorithms exist that can compute the GCD faster than the quadratic runtime associated with the Euclidean method when dealing with larger inputs.

Areas of Agreement / Disagreement

Participants express differing views on the algorithms used for GCD computation, with no consensus on a single method or approach being universally accepted.

Contextual Notes

Some limitations include the potential for varying performance based on the algorithm chosen and the input size, as well as the dependence on specific programming implementations.

Who May Find This Useful

Readers interested in computer science, algorithm design, programming practices, and mathematical software development may find this discussion relevant.

matqkks
Messages
283
Reaction score
6
Do they use the Euclidean Algorithm?
 
Mathematics news on Phys.org
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   Reactions: berkeman
Check the rosettacode.org site someone may have developed the code in whatever language you are interested in.
 
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   Reactions: Janosh89 and berkeman
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   Reactions: jedishrfu

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 13 ·
Replies
13
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
5
Views
2K