GCD of n Numbers

30
Saikat Roy Jadavpur University 1 Generalized GCD

description

A discussion about the GCD of n numbers. Algorithms discussed with examples are Djikstra's Algo, Euclidean Algo, Binary GCD Algo, Lehmar's GCD Algo and Daykin's GCD Algo.

Transcript of GCD of n Numbers

Page 1: GCD of n Numbers

Jadavpur University

Saikat Roy

1

Generalized GCD

Page 2: GCD of n Numbers

Jadavpur University

Contents

2

GCD and its basic propertiesBasic Problem and GCD of n numbersStandard GCD algorithmsAn Interesting GCD Algorithm & FactsPerformance for n numbersApplication of GCD algorithmsReferences

Page 3: GCD of n Numbers

Jadavpur University

Greatest Common Divisor

3

Greatest Common Divisor or GCD is the largest positive integer that divides 2 or more numbers without leaving a remainder.GCD(20,12) = 4, GCD(14,28,30) = 2

GCD (A,B) = GCD ( |A| , |B|)GCD (A,B) 1 , if either A or B 0GCD (A,B) = 1, implies A and B are co-prime.

Page 4: GCD of n Numbers

Jadavpur University

Basic Problem and GCD of n numbers

4

GCD of very large numbers is an issueGCD(24, 12) is easy.GCD(1000020, 6748966) is not.

Extending the Problem to GCD of n numbers is another issue.

It can be proved that the problem is reducible to continuously taking GCD of 2 numbers at a time.

Page 5: GCD of n Numbers

Jadavpur University

Problem Simplification

5

Any positive integer can be expressed in the formA = 2x13x25x3 …. =

GCD(A,B) = GCD(A,B,C) =

min(X,Y,Z) = min (min(X,Y), Z))Therefore,GCD(A,B,C) = =GCD((A,B), C)

There the problem of GCD of n numbers can be reduced into taking GCD of 2 numbers at a time.

Page 6: GCD of n Numbers

Jadavpur University

Standard GCD Techniques

6

Brute ForceDjikstra’s or Bishop’s AlgorithmEuclidean AlgorithmBinary GCD Algorithm (Stein’s Algorithm)Lehmar’s Algorithm

Each of these techniques were developed to exploit particular properties of GCD.

Page 7: GCD of n Numbers

Jadavpur University

Brute Force

7

Iterate from the smaller of the 2 numbers up to 1.

Stop when the number divides without remainder both numbers. That is the GCD.

Very simple.Extremely computationally expensive for

large numbers.

Page 8: GCD of n Numbers

Jadavpur University

Djikstra’s/Bishop’s Algorithm

8

If GCD(A,B) = K, where A B=> A = mK , B = nK=> A-B = (m-n) KTherefore, K is also a divisor of A-B.

Basically involves reducing the larger of the numbers A & B to A-B and continuing the GCD computation.

Page 9: GCD of n Numbers

Jadavpur University

Example of Bishop’s Algo.

9

GCD(64, 18) = ?

Therefore,GCD(64,18) = GCD(64-18,18) = GCD(46,18)

=GCD(28,18)=GCD(10,18) [Adjust using for eg. recursive function calls]=GCD(10,8)=GCD(2,8)Now since 8 is divisible by 2, GCD(64,18) = 2

Page 10: GCD of n Numbers

Jadavpur University

Euclidean Algorithm

10

THE algorithm when it comes to GCD and was proposed by Euclid about 2250 years ago.

Basically involves a radical reduction in problem size by changingGCD(A,B) => GCD(B, A mod B) where A>B.

It can be categorically proven that this is valid.

Page 11: GCD of n Numbers

Jadavpur University

The Proof

11

Let the problem be GCD(A,B) where A and B are non negative integers.Suppose we reduce the problem by dividing A by B.Therefore, A = k1B + R1 -------- (1)

Thus, we now take B and R1 and further reduce like above.Therefore, B = k2R1 + R2 -------(2)

So we have R1 and R2. Now lets suppose, R2 finally divides R1 without leaving a remainder.

Therefore, R1 = k3R2 ----------(3)

Page 12: GCD of n Numbers

Jadavpur University

The Proof (Continued)

12

Putting the values of Eqn (3) in Eqn. (2) we get,We have, B = k2k3R2 + R2

Þ B = R2 (1+k2k3)

Similarly from Eqn (1),We Have, A = k1 (1+k2k3) R2 + k3R2

A = R2 [k1 (1+k2k3) + k3]

Therefore, we can conclude R2 will divide both A and B without leaving a remainder.

Page 13: GCD of n Numbers

Jadavpur University

The Proof (Continued)

13

Suppose there is a greater number than R2 that divides A and B. Let it be X.Therefore, A = n1X and B = n2X , with X>R2

Putting new values of A and B in Eqn. (1) we have,A = k1B + R1 => n1X=k1n2X+R1 => R1=(n1-k1n2)X

Therefore, R1 is divisible by X.

Similarly putting new values of A, B and R1 in Eqn. (2),

R2 = [k2(n1-k1n2) - n2]X

Therefore R2 is divisible by X.

But X > R2 , so X cannot divide R2.

By contradiction, GCD(A,B) = R2

Page 14: GCD of n Numbers

Jadavpur University

Example of Euclidean Algo.

14

GCD(1000,120) = ?Then, GCD(1000,120) = GCD(120,40)Since 40 divides 120, GCD(1000,120) = 40

GCD(1230,235) = ?Then, GCD(1230,235) = GCD(235,55) =GCD(55,15) = GCD(15,10) =GCD(10,5)Since 5 divides 10, GCD(1230,235) = 5

Page 15: GCD of n Numbers

Jadavpur University

Binary GCD Algorithm

15

The Binary GCD Algorithm uses a few simple properties of GCD and bitwise & shift operators to carry out majority of its operations.If A and B are both even then, GCD(A,B) = 2*GCD(A/2,B/2)If either of A or B is even then,GCD(A,B) = GCD(A/2,B) or GCD(A,B/2)If both A and B are not even then,GCD(A,B) = GCD((A-B)/2,B) , where A>B

continue till A=B, and GCD(A,B) = B*2k

where k is the number of common factors of k.Faster than Euclidean on certain architectures. Comparable.

Page 16: GCD of n Numbers

Jadavpur University

Example of Binary Algo.

16

GCD(200, 172) = ?

2 x GCD(100,86)= 22 x GCD(50,43)= 22 x GCD(25,43)= 22 x GCD(25,9)= 22 x GCD(8,9)= 22 x GCD(4,9) = 22 x GCD(2,9)= 22 x GCD(1,9) = 22 x GCD(1,4)= 22 x GCD(1,2) = 22 x GCD(1,1) = Stop AlgorithmTherefore, GCD(200, 172) = 22 x 1 = 4

Page 17: GCD of n Numbers

Jadavpur University

Lehmar’s Algorithm

17

Lehmar’s Algorithm is a modification on the Euclidean Algorithm designed to work at very high speeds even for very large numbers requiring multi-precision operations.

Categorically more complex than the very simple Euclidean Algorithm.

The basic theory is this that for 2 equally large numbers, the 67.7% of the time the quotients are small – 1,2 or 3 to be precise (Knuth). So using a few leading digits, the steps of the Euclidean algorithm can be calculated with single precision calculations.

Page 18: GCD of n Numbers

Jadavpur University

Example of Lehmer’s Algorithm

18

u’ v’ q’ u’’ v’’ q’’

2718 1001 2 2719 1000 2

1001 716 1 1000 719 1

716 285 2 719 281 2

285 146 1 281 157 1

146 139 1 157 124 1

139 7 19 124 33 3

Let us consider a pair of 8 digit numbers, A = 27182818 and B = 10000000 on a machine which has only 4 digit words (upto 4 digit is single precision).

Let the numbers A and B be divided into 2 parts of 4 digit words.u’ = 2718, v’ = 1000 + 1 = 1001u’’ = 2718 + 1 = 2719 v’’ = 1000

Page 19: GCD of n Numbers

Jadavpur University

Example of Lehmer’s Algorithm(Contd.)

19

u v qA B 2

A A – 2B 1

A – 2B – A + 3B 2

– A + 3B 3A – 8B 1

3A – 8B – 4A + 11B 1

– 4A + 11B 7A – 19B ?

Therefore now apply the transformation of the last row on the original numbers.Anew = –4A + 11B = 1268728 and Bnew = 7A – 19B = 279726 and take them as the new numbers and continue the Algorithm as before.

Page 20: GCD of n Numbers

Jadavpur University

Interesting GCD Facts

20

The probability for 2 randomly selected numbers to have a GCD of 1 is approximately 0.6.

Suppose the probability of a number to be divided by a prime number p is .For eg. 7 divides one of every 7 numbers without a remainder.So for 2 numbers to be divided by p, the probability is Thus, for at least one number to be not divided the probability is Therefore, for all primes the probability that at least one of the 2 number is not divided by them, that is, the numbers are co-prime are = = 60% [Refer to Dirichlet, 1849]

Page 21: GCD of n Numbers

Jadavpur University

Interesting GCD Facts(Continued)

21

Probability that 2 randomly selected numbers are co-prime is approximately 0.6.

Owing to this fact, for a randomized set of N numbers, there is probability of 0.84 that the algorithm encounters a termination after only 2 iterations.

0.6 x 0.6 + 0.4 x 0.6 + 0.6 x 0.4 = 0.84

Based on this there actually is a probabilistic GCD algorithm for N numbers.

Page 22: GCD of n Numbers

Jadavpur University

Daykin’s GCD Algorithm

22

An esoteric GCD Algorithm.Can compute GCD using very simplistic operations.

The technique is as follows:To calculate GCD(A,B), check if either A divides B. Then A is the GCD. Otherwise do as follows.

Take 2 numbers N and Z.Initialize N = 10k – A, where k is the least positive integer such that 10k > A.Initialize, Z = B.

Page 23: GCD of n Numbers

Jadavpur University

Daykin’s Algo.(Continued)

23

The next number is the N+Z.All carries beyond k digits are ignored.

If there is a carry, it is a Z number otherwise it is an N number.

Continue this until the next number is 0.The last Z number is the GCD of A and B.

Example on next slide.

Page 24: GCD of n Numbers

Jadavpur University

Example of Daykin’s Algo.

24

GCD(110 , 68) = ?

Then k = 3.N = 10k – 110 = 1000 – 110 = 890 and Z = 68

Next = N+Z = 890 + 68 = 958 = NN = 958, Z = 68

Next = N+Z = 958 + 68 = 1026 = ZN = 958, Z = 26

Next = N+Z = 958 + 26 = 984 = NN = 984, Z = 26

Page 25: GCD of n Numbers

Jadavpur University

Example(Continued)

25

N = 984, Z = 26

1010 = Z (N = 984, Z = 10) 994 = N (N = 994, Z = 10) 1004 = Z (N = 994, Z = 4) 998 = N (N = 998, Z = 4) 1002 = Z (N = 998, Z = 2)

1000 = STOP THE ALGORITHM

GCD is the last Z number.

GCD(110 , 68) = 2

Page 26: GCD of n Numbers

Jadavpur University

Performance on 2 numbers

26

A test on 2 large numbers of 10 and 9 digits respectively with a GCD of a single digit.GCD(1000000000,450000044) = 4

It is obvious that the Daykin’s Algorithm through its unique use of simplistic operations is faster than the Brute force and Bishop’s GCD techniques.Only the Stein’s Binary GCD Algorithm comes close to the Euclidean Algorithm.

Brute Force Bishop's Algo Daykin's Algo Euclidean Algo Stein's Algo

2.61 secs 0.00067 secs 0.00026 secs 0.00000022 secs 0.00000060 secs

Page 27: GCD of n Numbers

Jadavpur University

Performance on n numbers

27

For the lack of a better test case generator, a set of 10000 large even numbers of 5-6 digits each has been taken as the test case.

It is obvious that this does not correspond to previous results for 2 numbers.

This is due to the tendency of random numbers to be co-prime.

Hence the GCD calculation usually scales down very fast after only a few iterations.

Brute Force Bishop's Algo Daykin's Algo Euclidean Algo Stein's Algo0.001 secs 0.001 secs 0.001 secs < 10-18

secs0.001 secs

Page 28: GCD of n Numbers

Jadavpur University

Applications of GCD

28

Particularly, the Euclidean Algorithm has uses ranging from computing Polynomial GCD, multiplicative inverses which is important in RSA algorithm, finding the any number from the root in a Stern-Brocot tree etc.

The fundamental concept of GCD is useful starting from grade school mathematics and extends to understanding concepts of Missing Fundamentals in sound processing.

Page 29: GCD of n Numbers

Jadavpur University

References

29

Donald Knuth, The Art of Computer Programming, Seminumerical Algorithms.

Gene Cooperman, GCD of Many Integers.D.E. Daykin, An Addition Algorithm for

Greatest Common Divisor.Bruce Ikenage, Greatest Common Divisor.Wikipedia

Page 30: GCD of n Numbers

Jadavpur University

THANK YOU

30