Lecture 5 - i
Download
Report
Transcript Lecture 5 - i
LECTURE 5
Learning Objectives
To apply division algorithm
To apply the Euclidean algorithm
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
RMIT University; Taylor's College
Activity 1
Card 2
Card 1
Card 4
Card 3
8
9
4
5
2
3
1
3
10
11
6
7
6
7
5
7
12
13
12
13
10
11
9
11
14
15
14
15
14
15
13
15
Pick
Is it
Is it
Is it
Is it
a integer between 0 to 15
on Card A?
on Card B?
on Card C?
on Card D?
RMIT University; Taylor's College
Activity 2
• Write a set of instructions (algorithms) to write all the
integers from 0 to 10.
RMIT University; Taylor's College
Algorithm example 1
• Step 1: Set 𝑥 = 0
• Step 2: 𝑤𝑟𝑖𝑡𝑒 𝑥.
• Step 3: 𝑥 = 𝑥 + 1
• Step 4: 𝑖𝑓 𝑥 = 10, stop
• Step 5: Go to Step 2
The Division Algorithm
The Division Algorithm
• For any integer 𝑎, we can represent a in the form of
𝑎 = 𝑏𝑞 + 𝑟
where 0 ≤ 𝑟 < 𝑏.
•
•
•
•
a – integer
b – integer > 0
q – quotient
r – remainder
RMIT University; Taylor's College
Algorithms
𝑎 = 𝑏𝑞 + 𝑟
• The process of expressing a in this way is the application
of the division algorithm
• Essentially this says that we can divide one integer by
another if the latter is positive, and that we get a quotient
and a remainder
RMIT University; Taylor's College
The Division Algorithm
• Write the following integers in the form of 𝑎 = 𝑏𝑞 + 𝑟
1.
2.
3.
4.
𝑎 = 31, 𝑞 = 7
𝑎 = 42, 𝑞 = 4
𝑎 = 61, 𝑞 = 3
𝑎 = 1482, 𝑞 = 57
The Division Algorithm
• If a > 0, then
a
q
b
• Example: a = 31, b = 7
(floor of a/b)
31 3
q 4 4.428571 4
7 7
• So a = bq + r gives 31 = 7 ∙ 4 + 3
• Given a, b:
a
q
b
r a bq
Valid input requires a, b to be
integers and b > 0
The Euclidean Algorithm
Factors (or Divisors) and Multiple
Let a, b and c be integers.
• Suppose that ab = c. .
• We say that c is a multiple of a and of b.
• Also, a and b are divisors or factors of c.
• Example: (3)(5) = 15
• 15 is the multiple of 3 and of 5.
• 3 and 5 are divisors (factors) of 15.
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.
2.
What is the common factor for 16 and 24
What is the common factor for 15 and 30
RMIT University; Taylor's College
Common Multiple
•
A positive integer p is a common multiple of m and n if it is a
multiple of both of them
•
Examples:
Which of the following is the common multiple of 3 and 6?
1.
1.
2.
3.
4.
15
18
24
27
Which of the following is the common multiple of 4 and 9?
2.
1.
2.
3.
4.
36
54
72
108
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
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.
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.
2.
What is the LCM of 8 and 6?
What is the LCM of 3 and 4?
mn
lcm(m, n)
gcd( m, n)
The least common multiple of 2
positive integers equals their
product divided by their greatest
common divisor
Euclidean Algorithm
• We can get the gcd by
using the Euclidean
algorithm.
• This involves repeated
application of the division
algorithm: a = bq + r
When the remainder becomes zero, we
look back to the previous remainder, rn+1.
This must be the gcd of a and b.
• Euclidean Algorithm
a q0b r1
b q1r1 r2
r1 q2 r2 r3
rn 1 qn rn rn 1
rn qn 1rn 1
RMIT University; Taylor's College
Example 2
gcd (96, 22) = ?
96 = 4 ∙ 22 + 8
22 = 2 ∙ 8 + 6
The last nonzero remainder was 2.
Therefore, gcd (96, 22) = 2.
lcm(96,22)
8=1∙6+2
6=3∙2
No remainder
96 22
96 22
1056
gcd( 96,22)
2
RMIT University; Taylor's College
Example 3
gcd (63, 256) = ?
256 = 4 ∙ 63 + 4
The last nonzero remainder was 1.
Therefore, gcd (63, 256) = 1.
63 = 15 ∙ 4 + 3
63 256
lcm (63,256)
gcd( 63,256)
63 256
16,128
1
4=1∙3+1
3=3∙1
No remainder
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
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
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
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.
RMIT University; Taylor's College
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
BBC News (online) dated 5th December 2001
http://news.bbc.co.uk/2/hi/science/nature/1693364.stm, accessed 1st September 2009
Prime Number
• How do you determine a prime number?
• Principle
Prime number is an integer ≥ 2, that is only divisible by 1 and the
integer itself.
• If an integer is divisible by integers other than 1 and itself, it is not a
prime number.
• Example:
Is 357 a prime number?
Is 271 a prime number?
Prime Number
• Is 357 a prime number?
• Solution:
is 357 a prime number?
• 357 ÷ 2 = 178.5
357 is not divisible by 2.
• 357 ÷ 3 =119
357 is divisible by 3, it is not a prime number.
Prime Number
• Is 271 a prime number?
• Solution:
• 271 ÷ 2 = 135.5
271 is not divisible by 2.
• 271 ÷ 3 = 90.3333
• 271 is not divisible by 3.
• 271 ÷ 5 = 54.2
• 271 is not divisible by 5
• …Continue to divide 271 by all the odd integers. We find that 271 is
not divisible by any integer.
• Conclusion: 271 is prime number.
Prime Number
• Algorithm to determine a prime number.
• Assign
t = an integer to be tested,
d= integer as divisor
•
•
•
•
•
•
•
Step 1: Set d = 2
Step 2: if t mod d = 0, then t is not a prime number. Stop.
Step 3: Set d = 3
Step 4: if t mod d = 0, then t is not a prime number. Stop.
Step 5: d = d + 2
Step 6: if 𝑑 ≤ 𝑡 − 1 go to step 4
Step 7: t is a prime number.
Activity 3
• Write down the first ten prime numbers.
•
•
•
•
•
•
•
•
•
•
2
3
5
7
11
13
17
19
23
29
The End
RMIT University; Taylor's College
Prime Number
• Algorithm to determine the first 10 prime numbers.
• Assign
t = an integer to be tested,
d = integer as divisor
• Step 1: Set t = 2
• Step 2: Set d = 2
• Step 3: Do step 4 to step if d < t. Else, t is a prime number. Go to Step
•
•
•
•
•
•
.
Step 4: if t mod d = 0, then t is not a prime number. Stop.
Step 5: Set d = 3
Step 4: if t mod d = 0, then t is not a prime number. Stop.
Step 5: d = d + 2
Step 6: if 𝑑 ≤ 𝑡 − 1 go to step 4
Step 7: t is a prime number.