You are on page 1of 4

# M1C An Introduction To Maple

Phil Ramsden

## Assessed Project 2: Euclids Algorithm

Deadline: 29 and 30 November 2012 Your coursework must contain a cover sheet and the following signed pledge that it is your own work. This signature signies that this represents my own work, completed independently, without assistance except where acknowledged and properly referenced. Signed: CID: Name: Tutorial Group:

This assignment, together with all the other resources youll need, are available in Blackboard, at http://learn.imperial.ac.uk/
A Very, very important. The online resources, including this document, were written in LTEX, and the text contains certain characters that Maple doesnt recognise, and that can corrupt your les. So be very careful about copying and pasting text from them into Maple; indeed, avoid doing so wherever you can. Copying and pasting Maple code is ne.

Part 1
The highest common factor (HCF) or greatest common divisor (GCD) of two numbers is the highest number that goes into them both. Thus the HCF of 36 and 60 is 12, and the HCF of 35 and 60 is 5. One way to nd it is to do a prime factorisation of both numbers and then see what factors occur in both. However, once numbers get at all large, factorisation starts to take a long time: no genuinely rapid algorithms for factorisation are known (and it may well be that none exist). So if we want a rapid algorithm for nding HCFs, it must be one that avoids integer factorisation. Amazingly, such an algorithm has been known for over two millennia; its called Euclids algorithm. To nd the HCF of a and b, we Set r1 = a, r2 = b. For each i >= 2, let ri+1 be the remainder when ri1 is divided by ri . When ri+1 = 0, the HCF is ri . 1

Question 1
(i) Write some Maple code (for full marks, a procedure) that takes two positive integers and returns their highest common factor. (ii) Test your code on the integers a = 188613 and 136458; the HCF should be 171. (iii) Rewrite your code so that it displaysor, for full marks, returnsall the intermediate remainders as well as the nal highest common factor. How many steps does the algorithm take to get to 171? (iv) Use your code on the Fibonacci numbers F27 = 196418, F26 = 121393: how many steps does this take? Show that this is the smallest pair of numbers to require this many steps. (Hint: imagine running the algorithm in reverse from 1, taking the worst case each time.)

Part 2
If h is the HCF of a and b, then there exist and such that h = a + b. So for example in the case a = 35, b = 60 we have a HCF of 5, and 5 35 + 3 60 = 175 + 180 = 5. Euclids algorithm can be extended to nd the values of and , as follows: Set r1 = a, 1 = 1, 1 = 0. Set r2 = b, 2 = 0, 2 = 1. For each i >= 2, let ri+1 be the remainder when ri1 is divided by ri . If the quotient from this division sum is qi , then let i+1 = i1 qi i ; i+1 = i1 qi i ; . At every stage, i a+i b = ri . When ri+1 = 0, the HCF is h = ri , and i a+i b = h.

Question 2
(i) Extend your Maple code to nd and as well as h. Test on the integers a = 188613 and 136458, showing all intermediate remainders and the associated values of and (again, for full marks these should be returned). (ii) Given that n = 211583 and e = 4177, nd d > 0 such that d e 1 (mod n). For full marks, you should do this using your program from part (i). The rst two questions carry 80% of the marks.