1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

I How do computers evaluate the GCD of two integers?

  1. Sep 12, 2018 #1
    Do they use the Euclidean Algorithm?
     
  2. jcsd
  3. Sep 12, 2018 #2

    mathman

    User Avatar
    Science Advisor

    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.
     
  4. Sep 12, 2018 #3

    jedishrfu

    Staff: Mentor

    Check the rosettacode.org site someone may have developed the code in whatever language you are interested in.
     
  5. Sep 12, 2018 #4

    jedishrfu

    Staff: Mentor

    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.
     
  6. Sep 13, 2018 #5

    mfb

    User Avatar
    2017 Award

    Staff: Mentor

    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.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted