Chapter 5: divisibility and the greatest common factor (2)


Euclidean algorithm

The most effective way to find the greatest common factor of two numbers is Euclidean algorithm, which consists of a series of division with remainder until the remainder is zero. Before describing the general method, we use an example to illustrate:

We calculategcd(36,132)The first step is132divide36Get quotient3And remainder24。 We write it down as

132 = 3·36 +24

The second step is to take36, using the remainder of the previous step24divide36have to

36 = 1·24 +12

Next, use12divide24Find the remainder0

24 = 2·12 + 0

Euclidean algorithm shows that when we get the remainder0Then the remainder of the previous step is the greatest common factor of the first two numbersgcd(132,36)=12

Pay attention to how to count at each stepAdivideBGet quotientQSum remainderRIn other words,

A = Q·B +R

Then count it in the next stepBAndRReplace the originalAAndBContinue the process until you get the remainder0until. At this point, the remainder of the previous stepRIs the greatest common factor of the first two numbers.

Euclidean algorithm: to calculate two numbersaAndbThe greatest common factor of, shillingsr_{-1}=a,r_0=bAnd then calculate the successive quotient and remainder

r_{i-1}=q_{i-1}·r_{i}+r_{i-1}(i = 0,1,2,…)

Until some remainderr_{n+1}by0。 The last nonzero remainderr_nnamelyaAndbMaximum common factor of

This work adoptsCC agreementThe author and the link to this article must be indicated in the reprint


Recommended Today

The actor model is so excellent under distributed high concurrency

Write at the beginning In general, there are two strategies for communicating in concurrent threads: shared data and messaging. One of the biggest problems of concurrent programming with shared data is data condition competition. It’s a headache to deal with all kinds of locks. Most of the traditional popular language concurrency is based on shared […]