• 沒有找到結果。

1.1 Sequential Search for Greatest Common Divisor

N/A
N/A
Protected

Academic year: 2022

Share "1.1 Sequential Search for Greatest Common Divisor"

Copied!
4
0
0

加載中.... (立即查看全文)

全文

(1)

Data Structures and Algorithms (NTU, Class 01/02, Spring 2011) instructor: Hsuan-Tien Lin

Homework #1

TA in charge: Yao-Nan Chen RELEASE DATE: 02/23/2011 DUE DATE: 03/15/2011, 17:20

As directed below, you need to submit your code to the designated place on the course website.

Any form of cheating, lying or plagiarism will not be tolerated. Students can get zero scores and/or get negative scores and/or fail the class and/or be kicked out of school and/or receive other punishments for those kinds of misconducts.

Discussions on course materials and homework solutions are encouraged. But you should write the final solutions alone and understand them fully. Books, notes, and Internet resources can be consulted, but not copied from.

Since everyone needs to write the final solutions alone, there is absolutely no need to lend your homework solutions and/or source codes to your classmates at any time. In order to maximize the level of fairness in this class, lending and borrowing homework solutions are both regarded as dishonest behaviors and will be punished according to the honesty policy.

Both English and Traditional Chinese are allowed for writing any part of your homework (if the compiler recognizes Traditional Chinese, of course). We do not accept any other languages. As for coding, either C or C++ or a mixture of them is allowed.

This homework set comes with 200 points and 10 bonus points. In general, every home- work set of ours would come with a full credit of 200 points.

Introduction

The greatest common divisor gcd(a, b) between two non-zero integers a and b is defined as the largest positive integer that divides both a and b without a remainder. Mathematically speaking, for any integer k > gcd(a, b), (a mod k) 6= 0 or (b mod k) 6= 0. In this homework set, we translate several properties of the gcd function to corresponding algorithms. In particular, we will pay some respect to Euclid’s algorithm, which is arguably one of the earliest algorithms ever.

1.1 Sequential Search for Greatest Common Divisor

(1) (10%) The following GCD-By-Def algorithm simply translates from the definition of gcd. Note that GCD-By-Def is not an algorithm yet because the for loop does not end, which violates the finiteness property of an algorithm. Provide an upper bound for i in the for loop that completes this algorithm. Assume that a > b. How many iterations of for does GCD-By-Def take?

GCD-By-Def(integer a, integer b) result ← 1

for i ← 2 to · · · do

if a mod i = 0 and b mod i = 0 then result ← i

end if end for return result

1 of 4

(2)

Data Structures and Algorithms (NTU, Class 01/02, Spring 2011) instructor: Hsuan-Tien Lin

(2) (10%) Dr. Speedup is not satisfied with the performance of GCD-By-Def, and thus designed another algorithm called GCD-By-Reverse-Search below. Prove that the algorithm is correct for positive integers a, b using the mathematical definition of gcd. (Hint: Proving by contradiction may be useful. If the returned value is not gcd(a, b), ...)

GCD-By-Reverse-Search(integer a, integer b) for i ← min(a, b) to 1 do

if a mod i = 0 and b mod i = 0 then return i

end if end for

(3) (10%) Assume that a > b. What is the maximum number of iterations that GCD-By-Reverse- Search needs? When does the situation happen? (The situation is usually called the worst case.) (4) (10%) Assume that a > b. What is the minimum number of iterations that GCD-By-Reverse- Search needs? When does the situation happen? (The situation is usually called the best case.)

1.2 Recursive Filtering for Greatest Common Divisor

Filtering algorithms try to extract small factors within the gcd using the following property.

(1) (Bonus 5%) If any positive integer ` divides both a and b, prove that gcd(a, b) = ` · gcd(a/`, b/`) using only the mathematical definition of gcd.

(2) (10%) Dr. Filter uses the definition above to design the following algorithm for gcd. Note that GCD-By-Filter is not an algorithm yet because there is a  in the last line, which violates the definiteness property of an algorithm. What should  be? Briefly argue that your choice of  makes GCD-By-Filter correct for positive integers a, b.

GCD-By-Filter(integer a, integer b) for i ← 2 to min(a, b) do

if a mod i = 0 and b mod i = 0 then return i ∗ GCD-By-Filter(a/i, b/i) end if

end for return 

(3) (10%) Dr. FastFilter wants to speed up GCD-By-Filter by adding a parameter s that limits the range of i so smaller integers that have been tested for division will not be tested again. Complete the algorithm GCD-By-Filter-Faster below by filling in the 4 part. Briefly argue that your choice of 4 makes GCD-By-Filter-Faster correct for positive integers a, b.

GCD-By-Filter-Faster(integer a, integer b)

return GCD-By-Filter-Faster-Internal(a, b, 4)

GCD-By-Filter-Faster-Internal(integer a, integer b, integer s) for i ← s to min(a, b) do

if a mod i = 0 and b mod i = 0 then

return i ∗ GCD-By-Filter-Faster-Internal(a/i, b/i, i) end if

end for return 

2 of 4

(3)

Data Structures and Algorithms (NTU, Class 01/02, Spring 2011) instructor: Hsuan-Tien Lin

1.3 Euclid’s Algorithm for Greatest Common Divisor

Euclid’s algorithm is based on the following property.

(1) (Bonus 5%) Formally prove that gcd(a, b) = gcd(b, a mod b) using only the mathematical defini- tion of gcd.

(2) (10%) Euclid’s algorithm, as you probably learned in high school, is as follows. Write down the values of m, n and tmp in each iteration when a = 21 and b = 13.

GCD-By-Euclid(integer a, integer b) n ← min(a, b), m ← max(a, b)

while m mod n 6= 0 do tmp ← n

n ← m mod n m ← tmp end while return n

(3) (10%) Assume that a and b are positive integers. Prove that GCD-By-Euclid satisfies the finiteness property. That is, the while only runs for a finite number of iterations for any given positive integer pair (a, b).

(4) (10%) The pseudo-code above does not work when a and b are negative. Write down the pseudo- code that can correctly work for negative a or b.

(5) (10%) Assume that it takes T iterations of while to run GCD-By-Euclid(a, b) for some positive integers a and b. How many iterations does it take to run GCD-By-Euclid(2a, 2b)? Formally prove your result.

1.4 Comparison of GCD

In all the implementations below, your code should be able to work correctly for any nonzero integers a and b.

(1) (10%) Implement GCD-By-Def and briefly (≤ 5 lines) state how you test whether your imple- mentation is correct.

(2) (10%) Implement GCD-By-Reverse-Search and briefly state how you test whether your im- plementation is correct.

(3) (10%) Implement GCD-By-Filter and briefly state how you test whether your implementation is correct.

(4) (10%) Implement GCD-By-Filter-Faster and briefly state how you test whether your imple- mentation is correct.

(5) (10%) Implement GCD-By-Euclid and briefly state how you test whether your implementation is correct.

(6) (20%) Consider a = 32460 and b ∈ {32000, 32001, 32002, · · · , 32460}. Plot b versus the number of iterations (of for or while) that GCD-By-Def, GCD-By-Reverse-Search, GCD-By-Filter, GCD-By-Filter-Faster and GCD-By-Euclid takes to compute gcd(a, b) as curves on the same figure. Briefly state your findings.

Please submit the code in this part as one single file hw1 4.c or hw1 4.cpp. The code should take two 4-byte integers a and b from the standard input per line until reaching a = 0. Then, the code should output the gcd of a and b computed by each algorithm and the number of iterations the algorithm takes.

3 of 4

(4)

Data Structures and Algorithms (NTU, Class 01/02, Spring 2011) instructor: Hsuan-Tien Lin

Sample Input 3 2

-2 3 0

Sample Output (Note that the AAA, BBB, · · · should be filled by the actual numbers.) Case (3, 2): GCD-By-Def = 1, taking AAA iterations

Case (3, 2): GCD-By-Reverse-Search = 1, taking BBB iterations Case (3, 2): GCD-By-Filter = 1, taking CCC iterations

Case (3, 2): GCD-By-Filter-Faster = 1, taking DDD iterations Case (3, 2): GCD-By-Euclid = 1, taking EEE iterations

Case (-2, 3): ...

1.5 GCD of Polynomials

(1) (30%) Let p(x) and q(x) be non-zero polynomials with real-valued coefficients. A greatest common divisor (gcdpoly) of p(x) and q(x) is a polynomial d(x) of highest degree such that d(x) is a divisor of p(x) and of q(x). It is well-known that GCD-By-Euclid can be extended to find gcdpoly.

Implement GCD-By-Euclid-Poly below with

#define MAXDEG (100) typedef struct {

double coef[MAXDEG+1];

} poly;

as your representation of polynomials. Print out n, m and tmp in your program and show how it computes the gcd of x5+ 15x4+ 85x3+ 225x2+ 274x + 120 and x3+ 10x2+ 27x + 18.

GCD-By-Euclid-Poly(polynomial a, polynomial b) n ← min(a, b), m ← max(a, b)

while n does not divide m do tmp ← n

n ← the remainder polynomial when m is divided by n m ← tmp

end while return n

Please submit the code in this part as a file hw1 5.c or hw1 5.cpp. The sample input and sample output will be explained on the course website.

Submission File

Please submit your written part of the homework on all problems together before the deadline in the Tuesday class (03/15/2011) or to the TA at CSIE R217. Also, you need to upload your coding part as a single ZIP compressed file to CEIBA. The zip file should be like b86506054.zip, where the file name should be changed to your own school ID. The ZIP file should contain the following items:

• hw1 4.c or hw1 4.cpp

• hw1 5.c or hw1 5.cpp

• an optional README, anything you want the TAs to read before grading your code

The TAs will use the Makefile provided on the course website to test your code. Please make sure that your code can be compiled with the Makefile on CSIE R217 linux machines.

4 of 4

參考文獻

相關文件

What is the maximum number of iterations that GCD-By-Reverse- Search needs (for a fixed a).. When does the

The greatest common divisor gcd(a, b) between two positive integers a and b is defined as the largest positive integer that divides both a and b without a remainder?. In the

Students may not be familiar with

For 5 to be the precise limit of f(x) as x approaches 3, we must not only be able to bring the difference between f(x) and 5 below each of these three numbers; we must be able

To find a formula that is more convenient than Equation 1 for computational purposes, we let a and b be the vectors that start at P ij and lie along the sides of the

function by using the ²-δ language?. Why does

If we can show that U 1 and U 2 are open, so is their union by the open set property (any union of open sets

[Hint: You may find the following fact useful.. If d is a metric for the topology of X, show that d|A × A is a metric for