130 likes | 138 Views
This text explores Euclid's Algorithm and its analysis in mathematics, including flowcharts and programming in Informatics. Learn its efficiency and comparison with SimpleGCD.
E N D
Abstract Informatics is concerned with such topics as the theory and properties of algorithms ,methods for the design and analysis of algorithms, programming and other languages for describing algorithms and so on. We now study these description schemes in more detail and also introduce several important analysis issues.
Euclid live in the city of Euclid is the most famous ancient Greek mathematician. With regard to the life we knows very little. About early schooling in Athens, and well aware of Plato's theory. Euclid’s algorithm ( Greatest Common Divisor) Euclid
Theorem 1.1 Given positive integers m and n ,there exist unique integers q and r such that q ≥0,0≤r<n, and m = q ×n+r Theorem 1.2 Given positive integers m and n and the unique integers q and r such that q ≥0,0≤r<n ,and m = q × n + r ,then (1) if r = 0,then gcd ( m ,n) = n ,and (2) if r≠0,then gcd (m ,n)=gcd (n ,r). ‘Greatest common divisor’ is based on the following two theorems.
Describe Euclid’s algorithm in mathematics. • For example ,let m = 20 and n = 8,then m = 2 × 8+4.therefore gcd(20,8) = gcd(8,4). • r0=m – q0 × n (0 ≤q0,0 <r0 <n) • r1 = n – q1 × r0 (0 ≤q1,0 <r1 <r0) • r2 = r0 – q2 × r1 (0 ≤q2,0 <r2 <r1) • ………… • ri = ri-2 – qi × ri-1 (0 ≤qi,0 <ri <ri-1)
Not an entirely evident translation of the mathematics • 1. Divide m by n ,and obtain the remainder r. • 2.if r = 0,then halt. The answer is n. • 3. Replace m by n (m ←n) and n by r (n ← r). • 4.Go to step 1.
Flowchart –Euclid’s Algorithm shows instructions in flow chart form Mod : take the remainder on dividing m by n
Another flow chart which is Correctness of Euclid’s Algorithm For example M=20 and N=8,yields the command execution sequence : {A0} C0 {A1} C1 {A2} C2 {A3} C3 {A5} C4 {A6} C1 {A2} C2 {A3} C3 {A4}
One Algolic program that directly follows the preceding descriptions for Euclid’s algorithm • 0 algorithm GCD0; • 1 m , n , r : integer • 2 begin • 3 read ( m , n); • 4 write(‘ m =‘, m ,’n =‘,n); • 5 l: r: = m mod n; • 6 if r≠0 thenbegin • 7 m: = n ; n : = r; • 8 go to l • 9 end; • 10 write(‘ The gcd is ’,n) • 11 end.
Another way to express the same algorithm as a program in a given language • 0 algorithm GCD1; • 1 m , n , r : integer; • 2 begin • 3 read ( m , n ) ; write (‘ m ’ , m , ’ n= ’,n); • 4 while n≠0 dobegin { Perform loop as long as n ≠0.} • 5 r: = m mod n; • 6 m: = n ; n: = r • 7 end ; { gcd is in m.} • 8 write ( ‘The gcd is ’,m) • 9 end.
A description that closely resembles the original mathematical basis of the algorithm • 0 functiongcd ( m , n : integer ) : integer; • 1 gcd:=if n = 0 then m • 2 elsegcd ( n , m mod n); • Recursive : invoke themselves.
Another way to solve the same problem • 1. Set g equal to n ( g ←n). • 2. If g divides m and n , then halt. G is the gcd. • 3. Decrement g by 1 (g ←g-1). • 4. Go to step 2.
Euclid’s algorithm is better than SimpleGCD • 0 algorithm SimpleGCD; • 1 …… • 2 g:=n; • 3 l: ifnot (m mod g = 0) and (n mod g=0) then begin • 4 {g is not a divisor of both m and n.} • 5 g:=g-1; • 6 goto l • 7 end; • 8 {g is the gcd.} • 9 …. • 10 end • Efficient ( in time ) : SimpleGCD tests every integer between n and the gcd ( m , n ),but Euclid’s algorithm use the remainder. • Complicated : SimpleGCD requiring two divisions ( m mod g and n mod g )for each candidate. And For example , gcd(20,8) is obtained in two passes through the loop with Euclid’s algorithm ,and five passes using SimpleGCD .