0% found this document useful (0 votes)
25 views2 pages

Binary GCD Algorithm: V, Where K Is The Number of Common Factors of 2 Found in Step 2

The binary GCD algorithm, also known as Stein's algorithm, computes the greatest common divisor (GCD) of two non-negative integers using simpler arithmetic operations than the conventional Euclidean algorithm. It replaces division with bit shifts, comparisons, and subtraction. The algorithm recursively applies identities to reduce the problem until one number is 0, at which point the GCD is the other number. It runs in O(n^2) time where n is the number of bits in the larger number.

Uploaded by

herman sugiharto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Binary GCD Algorithm: V, Where K Is The Number of Common Factors of 2 Found in Step 2

The binary GCD algorithm, also known as Stein's algorithm, computes the greatest common divisor (GCD) of two non-negative integers using simpler arithmetic operations than the conventional Euclidean algorithm. It replaces division with bit shifts, comparisons, and subtraction. The algorithm recursively applies identities to reduce the problem until one number is 0, at which point the GCD is the other number. It runs in O(n^2) time where n is the number of bits in the larger number.

Uploaded by

herman sugiharto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Binary GCD Algorithm

The binary GCD algorithm, also known as Stein's algorithm, is an algorithm that computes the
greatest common divisor of two nonnegative integers. Stein's algorithm uses simpler arithmetic
operations than the conventional Euclidean algorithm; it replaces division with arithmetic
shifts, comparisons, and subtraction. Although the algorithm was first published by the Israeli
physicist and programmer Josef Stein in 1967, it may have been known in 1st-century China.
algoritma GCD biner , juga dikenal sebagai algoritma Stein , adalah suatu algoritma yang
menghitung pembagi umum terbesar dari dua bilangan bulat non-negatif. Algoritme Stein
menggunakan operasi aritmatika yang lebih sederhana daripada algoritma Euclidean
konvensional ; ia menggantikan pembagian dengan pergeseran aritmatika , perbandingan, dan
pengurangan. Meskipun algoritma ini pertama kali diterbitkan oleh fisikawan dan programmer
Israel Josef Stein pada tahun 1967, itu mungkin telah dikenal di Cina abad ke-1.

The algorithm reduces the problem of finding the GCD by repeatedly applying these identities:
1. gcd(0, v) = v, because everything divides zero, and v is the largest number that
divides v. Similarly, gcd(u, 0) = u. gcd(0, 0) is not typically defined, but it is convenient
to set gcd(0, 0) = 0.
2. If u and v are both even, then gcd(u, v) = 2·gcd(u/2, v/2), because 2 is a common
divisor.
3. If u is even and v is odd, then gcd(u, v) = gcd(u/2, v), because 2 is not a common
divisor. Similarly, if u is odd and v is even, then gcd(u, v) = gcd(u, v/2).
4. If u and v are both odd, and u ≥ v, then gcd(u, v) = gcd((u − v)/2, v). If both are odd
and u < v, then gcd(u, v) = gcd((v − u)/2, u). These are combinations of one step of the
simple Euclidean algorithm, which uses subtraction at each step, and an application of
step 3 above. The division by 2 results in an integer because the difference of two odd
numbers is even.
5. Repeat steps 2–4 until u = v, or (one more step) until u = 0. In either case, the GCD is
2kv, where k is the number of common factors of 2 found in step 2.
The algorithm requires O (n2) worst-case time, where n is the number of bits in the larger of
the two numbers. Although each step reduces at least one of the operands by at least a factor
of 2, the subtract and shift operations take linear time for very large integers (although they're
still quite fast in practice, requiring about one operation per word of the representation).

Algoritme mengurangi masalah menemukan GCD dengan berulang kali menerapkan identitas
ini:
1. gcd (0, v ) = v , karena semuanya membagi nol, dan v adalah angka terbesar yang
membagi v . Demikian pula, gcd ( u , 0) = u . gcd (0, 0) biasanya tidak ditentukan,
tetapi lebih mudah untuk mengatur gcd (0, 0) = 0.
2. Jika u dan v keduanya genap, maka gcd ( u , v ) = 2 · gcd ( u / 2, v / 2), karena 2 adalah
pembagi umum.
3. Jika u genap dan v aneh, maka gcd ( u , v ) = gcd ( u / 2, v ), karena 2 bukan pembagi
umum. Demikian pula, jika u aneh dan v adalah genap, maka gcd ( u , v ) = gcd
( u , v / 2).
4. Jika u dan v keduanya ganjil, dan u ≥ v , maka gcd ( u , v ) = gcd (( u - v ) /
2, v ). Jika keduanya ganjil dan u < v , maka gcd ( u , v ) = gcd (( v - u ) / 2, u ). Ini
adalah kombinasi dari satu langkah dari algoritma Euclidean sederhana, yang
menggunakan pengurangan pada setiap langkah, dan aplikasi langkah 3 di
atas. Pembagian dengan 2 menghasilkan bilangan bulat karena perbedaan dua angka
ganjil adalah genap.
5. Ulangi langkah 2-4 sampai u = v , atau (satu langkah lebih) sampai u = 0. Dalam
kedua kasus, GCD adalah 2 k v , di mana k adalah jumlah faktor umum dari 2 ditemukan
pada langkah 2.
Algoritme membutuhkan O (n 2 ) waktu kasus terburuk, di mana n adalah jumlah bit yang
lebih besar dari dua angka. Meskipun setiap langkah mengurangi setidaknya satu operan
dengan setidaknya faktor 2, operasi pengurangan dan pergeseran memerlukan waktu linier
untuk bilangan bulat yang sangat besar (meskipun mereka masih cukup cepat dalam
praktiknya, membutuhkan sekitar satu operasi per kata dari representasi ).

def gcd(u, v):


if u == v:
return u
elif u == 0:
return v
elif v == 0:
return u
# u is even
elif u & 1 == 0:
# v is even
if v & 1 == 0:
return 2*gcd(u >> 1, v >> 1)
# v is odd
else:
return gcd(u >> 1, v)
# u is odd
elif u & 1 != 0:
# v is even
if v & 1 == 0:
return gcd(u, v >> 1)
# v is odd and u is greater than v
elif u > v and v & 1 != 0:
return gcd((u-v) >> 1, v)
# v is odd and u is smaller than v
else:
return gcd((v-u) >> 1, u)

You might also like