Download presentation
Presentation is loading. Please wait.
Published byArline Boone Modified over 9 years ago
2
LECTURE 5 Learning Objectives To apply division algorithm To apply the Euclidean algorithm
3
Algorithms An algorithm is a systematic procedures (instructions) for calculation. Algorithms are basic to computer programs. Essentially, a program implements one or more algorithms. Therefore, algorithmic complexity is important. In this Lecture, we will study a few algorithms: Division algorithm Euclidean algorithm Primality testing
4
Activity 1 Card 1 89 1011 1213 1415 RMIT University; Taylor's College Card 2 45 67 1213 1415 Card 3 23 67 1011 1415 Card 4 13 57 911 1315 Pick a integer between 0 to 15 Is it on Card A? Is it on Card B? Is it on Card C? Is it on Card D?
5
Activity 2 Write a set of instructions (algorithms) to write all the integers from 0 to 10. RMIT University; Taylor's College
6
Algorithm example 1 RMIT University; Taylor's College
7
The Division Algorithm
9
RMIT University; Taylor's College Algorithms
10
RMIT University; Taylor's College The Division Algorithm
11
If a > 0, then (floor of a/b) Example: a = 31, b = 7 So a = bq + r gives 31 = 7 ∙ 4 + 3 Given a, b: Valid input requires a, b to be integers and b > 0
12
The Euclidean Algorithm
13
Factors (or Divisors) and Multiple
14
Common Factor Let m, n be positive integers. A positive integer q is a common factor or common divisor of m and n if it divides (is a divisor, or factor, of) both of them Examples: 1. What is the common factor for 16 and 24 2. What is the common factor for 15 and 30
15
Common Multiple A positive integer p is a common multiple of m and n if it is a multiple of both of them Examples: 1. Which of the following is the common multiple of 3 and 6? 1. 15 2. 18 3. 24 4. 27 2. Which of the following is the common multiple of 4 and 9? 1. 36 2. 54 3. 72 4. 108 RMIT University; Taylor's College
16
Greatest Common Divisor (GCD) Let m, n be positive integers. The GCD (greatest common divisor) of m and n is the greatest number which is a common divisor of both of them It’s also called the highest common factor or HCF
17
Example 1 What is the GCD of 18 and 24? gcd (18, 24) = 6 ? There is a systematic procedure for getting the GCD. It’s the Euclidean algorithm.
18
Least Common Multiple Given integers m and n, their least common multiple (LCM) is the smallest number which is a multiple of them both Examples: 1. What is the LCM of 8 and 6? 2. What is the LCM of 3 and 4? The least common multiple of 2 positive integers equals their product divided by their greatest common divisor
19
Euclidean Algorithm We can get the gcd by using the Euclidean algorithm. This involves repeated application of the division algorithm: a = bq + r Euclidean Algorithm When the remainder becomes zero, we look back to the previous remainder, r n+1. This must be the gcd of a and b.
20
Example 2 RMIT University; Taylor's College gcd (96, 22) = ? 96 = 4 ∙ 22 + 8 22 = 2 ∙ 8 + 6 8 = 1 ∙ 6 + 2 6 = 3 ∙ 2 The last nonzero remainder was 2. Therefore, gcd (96, 22) = 2. No remainder
21
Example 3 RMIT University; Taylor's College gcd (63, 256) = ? 256 = 4 ∙ 63 + 4 63 = 15 ∙ 4 + 3 4 = 1 ∙ 3 + 1 3 = 3 ∙ 1 The last nonzero remainder was 1. Therefore, gcd (63, 256) = 1. No remainder
22
Extension to the Euclidean Algorithm If d = gcd(m, n) then d can be expressed as a linear combination d = xm + yn of m and n, where x and y are integers To find x and y, we work back through the steps of the Euclidean algorithm from bottom to top
23
Example 4 It can be shown that gcd(22, 96) = 2: 96 = 4 ∙ 22 + 8 22 = 2 ∙ 8 + 6 8 = 1 ∙ 6 + 2 6 = 3 ∙ 2 Now we want to express 2 as a linear combination 2 = x(22) + y(96). We use the second-last line to make 2 the subject of the equation: 2 = 8 – 1 ∙ 6 Next we use the third-last line to express 6 in terms of 22 and 8, substituting this into the equation we’ve just produced: 2 = 8 – 1 ∙ 6 = 8 – 1 ∙ (22 – 2 ∙ 8) = 8 – 1 ∙ 22 + 1 ∙ 2 ∙ 8 = 3 ∙ 8 – 1 ∙ 22
24
Example 4 (cont.) Finally we use the fourth-last line to express 8 in terms of 96 and 22, substitution this into our most recent equation 2 = 3 ∙ 8 – 1 ∙ 22 2= 3 ∙ (96 – 4 ∙ 22) – 1 ∙ 22 2= 3 ∙ 96 – 3 ∙ 4 ∙ 22 – 1 ∙ 22 2= 3 ∙ 96 – 13 ∙ 22 x=3, y=-4
25
Example 5 It can be shown that the gcd of 63 and 256 equals 1: 256 = 4 ∙ 63 + 4 63 = 15 ∙ 4 + 3 4 = 1 ∙ 3 + 1 3 = 3 ∙ 1 Then we work upwards from the second-last line, as follows: 1 = 4 - 1 ∙ 3 = 4 – 1 ∙ (63 – 15 ∙ 4) = 4 - 1 ∙ 63 + 1 ∙ 15 ∙ 4 = 16 ∙ 4 – 1 ∙ 63 = 16 ∙ (256 – 4 ∙ 63) – 1 ∙ 63 = 16 ∙ 256 – 64 ∙ 63 - 1 ∙ 63 = 16 ∙ 256 – 65 ∙ 63 So 1 = 16 ∙ 256 – 65 ∙ 63. In this example, 63 and 256 are relatively prime.
26
Prime Numbers A prime number is an integer ≥ 2 which has no factors except itself and 1 Prime numbers: 2, 3, 5, 7, … Prime numbers play a vital role in coding and cryptography We say two positive integers are relatively prime (in relation to each other) if their gcd equals 1 So 63 and 256 are relatively prime (to each other), even though neither of them is a prime number RMIT University; Taylor's College
27
http://news.bbc.co.uk/2/hi/science/nature/1693364.stmhttp://news.bbc.co.uk/2/hi/science/nature/1693364.stm, accessed 1 st September 2009 BBC News (online) dated 5 th December 2001
28
Prime Number
32
Activity 3 Write down the first ten prime numbers. 2 3 5 7 11 13 17 19 23 29
33
The End
34
RMIT University; Taylor's College
35
Prime Number
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.