Download Iterative and recursive versions of the Euclidean algorithm

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Large numbers wikipedia , lookup

Big O notation wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Arithmetic wikipedia , lookup

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Transcript
Iterative and recursive versions of the
Euclidean algorithm
Definition
The greatest common divisor g is the largest natural number that divides both a and
b without leaving a remainder.
GCD (24,18) = 2 · 3 = 6
A practical and easy way to calculate the greatest common divisor of two integer
numbers a and b is Euclidean algorithm. The algorithm does not require integer
factorization. It is one of the oldest algorithms named after by Euclid who described
it in his Elements in 300 BC.
Iterative realisation of the GCD algorithm
The Euclidean Algorithm
Example 1
The Euclidean algorithm is based on a principle that if a smaller number a is
subtracted from the larger number b, the smaller number and the difference will have
a common greatest common divisor as numbers a and b. If the following subtraction
yields a pair of equal numbers, it means that the GCD has been found.
Specification
Input data:
a, b ϵ N-{0} – for which we seek GCD.
Output data:
c ϵ N - GCD(a,b).
Steps:
Step 1: If a > b, then a = a - b otherwise b = b - a
Step 2: If a ≠ b execute step 1
Step 3: Write a
Step4: Stop
Flowchart
Start, give, write, stop
Pseudocode
while a ≠ b do
if a > b then
a = a - b
else
b = b - a
end if
end while
return a
Source code
View/Hide
euclid1.cpp, give two natural numbers
Example 2
The first example of finding GCD is bad mainly because of productivity. For example,
if the number a equals 2 millions and number b equals 2, the statement of a-b
subtraction will be executed till the variable a equals variable b – there will be about 1
million of such operations. It was also Euclid who noticed that the modulo operation
can be applied instead of subtraction.
Specification
Input data:
a, b ϵ N – for which we seek GCD.
Output data:
c ϵ N - GCD(a,b).
Steps
Step
Step
Step
Step
1:
2:
3:
4:
Calculate c when dividing a by b.
substitute number a with number b, and number b with number c
If number b = 0, then seeking GCD = a, otherwise go to step 1
Stop
Flowchart
Start, give, type, stop
Pseudocode
while b ≠ 0 do
c = a mod b
a = b
b = c
end while
return a
Source code
View/Hide
give two natural numbers
Example 3
Binary GCD algorithm
There is an algorithm that seeks GCD which is more effective than the two examples
mentioned above. The algorithm exploits the binary representation used by
computers and gains efficiency over the Euclidean algorithm by replacing divisions
and multiplications with shifts, even on a simple computer hardware. As a result we
obtain a 60%growth of the speed with which GCD is calculated with relation to the
standard Euclidean algorithm.
In order to apply the binary algorithm you need to learn the rules mentioned below:
1. GCD(0,b) = b, GCD(a,0) = a.
Every integer number can be zero’s divider. GCD(0,0) is not defined.
2. If numbers a and b are even, then GCD(a b) = 2·GCD(a/2,b/2).
3. In number a is even and number b is odd, then GCD(a,b) = GCD(a/2,b).
If number a is odd and number b is even, then GCD(a,b) = GCD(a,b/2).
4. If both numbers a and b are odd, and a ≥ b, then GCD(a,b) = GCD((a−b)/2,b).
If both numbers a and b are odd and a < b, then GCD(a,b) = GCD((b-a)/2,a).
5. Points 3 and 4 need to be repeated till you get b = 0. Then GCD equals
a·pow(2,k), where k corresponds to the number of common factors of number
2 from point 2. Multiplication of pow(2,k) is done with the use of arithmetic shift
operations of a variable a by k positions to the left.
Specification
Input data:
a, b ϵ N – for which we seek GCD.
Output data:
c ϵ N - GCD(a,b).
Helper variables:
k ϵ N – stores the number of common dividers of number 2
i ϵ N – used to store the difference between a and b.
Steps
; GCD(0,b) = b
Step 1: If a = 0, then write b and stop
; GCD(a,0) = a
Step 2: If b = 0, then write a and stop
; Iniciate the number of common dividers of number 2
Step 3: k = 0
; common factors of 2 are removed from a and b and the number of them is
memorised in k.
Step 4: As long as a and b are even do the steps: 5 - 7
Step 5: Shift bits a by 1 to the right
Step 6: Shift bits b by 1 to the right
Step 7: Increase k o 1
; GCS(0,b) = b
Step 8: If a = 0, then a = b and go to step 18
; dividers of 2 are eliminated from a
Step 9: As long as a is even a shift bits a by 1
; Dividers of 2 are elimiated from b, now a and b are odd
Step 10: As long as b is even shift bits b by 1
Step 11: If a ≥ b, go to step 16
; GCD (a,b) = GCD((b-a)/2,a)
Step 12: Suppose i = (b - a) shr 1
Step 13: Swap b with a
Step 14: Swap a with i
Dtep 15: execute step 17
; GCD(a,b) = GCD((a-b)/2,b)
Krok 16: Suppose a = (a - b) shr 1
Step 17: If b ≠ 0, go to step 8
; shift bits a by k places to the left - mnożenie przez pow(2,k)
Step 18: If k > 0, then a = a shl k
Step 19: Type a
Step 20: Stop
Source code
View/Hide
Give natural numbers
Recursive method of the GCD algorithm
To find a recursive version of the GCD algorithm one can use the formula below:
The formula is a recursive version of the Euclidean algorithm which calculates the
greatest common divider for two natural numbers a and b.
Algorithm realised by the function GCD(a,b):
1. if b = 0, then GCD(a,b) = a,
2. if b >= 1, then GCD(a,b) = GCD(b,a mod b).
For example according to recursive Euclid algorithm: GCD(24,18) = GCD(18,6) =
GCD(6,0) = 6.
Specification
Input data:
a, b ϵ N - dla których poszukujemy NWD.
Output data:
GCD(a,b) ϵ N.
Source code
View/Hide
Exercise 5.1.1.
Modify the above program so that the function GCD is defined according to the
formula below:
Comparison of the iterative and recursive versions of the
Euclidean algorithm
It is quite common that the implementation of the recursive algorithm is much easier
than implementation of the iterative algorithm. However, the ease is connected with
productivity. Usually the recursive algorithm is slower than the iterative one.
Let’s compare the two ways of implementation:
- method 2 (iteration)
- the only described method of recursion
Both methods perform the same operation – modulo.
To improve the clarity in both programs the statements have been reduced:
cout << "Give two natural numbers: ";
For numbers 1000 and 252 the difference in speed is not noticeable:
Exercise 5.1.2.
Check if efficiency of two Euclidean algorithms (iterative and rcursive) with the use of
two numbers is close.