Uploaded by tayyaba anwer

quiz1 sol

advertisement
Quiz 1 Solution
Find Euclid’s algorithm, consecutive integer checking algorithm, and middle-school
precedure of gcd(60,24) and find the number of steps used until the algorithm stops.
1.
Euclid’s algorithm
Step 1 If n = 0, return m and stop; otherwise go to Step 2
Step 2 Divide m by n and assign the value fo the remainder to r
Step 3 Assign the value of n to m and the value of r to n. Go to Step 1
Psuedo code
//computes gcd(m,n) by Euclid’s algorithm
//input: two nonnegative, not-both-zero integers m and n
//output: greatest common divisor of m and n
while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
So, It takes 9 steps and 3 repetitions.
2.
Find consecutive integer checking algorithm of gcd(60,24) and find the
number of steps used until the algorithm stops.
Step 1 Assign the value of min{m,n} to t
Step 2 Divide m by t. If the remainder is 0, go to Step 3;
otherwise, go to Step 4
Step 3 Divide n by t. If the remainder is 0, return t and stop;
otherwise, go to Step 4
Step 4 Decrease t by 1 and go to Step 2
Soln
The algorithm will try first 24, then 23 and so on until it reaches 12 (t = 12), where it
stops.
Then we get 29 steps or 13 repetitions.
3.
Middle-school procedure
Step 1
Step 2
Step 3
Step 4
Find the prime factorization of m
Find the prime factorization of n
Find all the common prime factors
Compute the product of all the common prime
gcd(m,n)
factor and return it as
1. Find the middle-school procedure for gcd(60,24)
2. Is this an algorithm?
Soln
step1 60 = 2 · 2 · 3 · 5
step2 24 = 2 · 2 · 2 · 3
step3 common prime factors = 2, 2, 3
step4 gcd(60, 24) = 2 · 2 · 3 = 12
The middle-school procedure is not an algorithm because the prime factorization
steps are not defined unambiguously.
The steps are a list of prime numbers!!!
Download