Wednesday, June 29, 2016

Calculate greatest common divisor

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

The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number.

A more efficient version of the algorithm shortcuts these steps, instead replacing the larger of the two numbers by its remainder when divided by the smaller of the two (with this version, the algorithm stops when reaching a zero remainder). With this improvement, the algorithm never requires more steps than five times the number of digits (base 10) of the smaller integer. 

Note that we ignored the quotient in each step except to notice when the remainder reached 0, signalling that we had arrived at the answer.

No comments: