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.

## Part 3: Advanced Material

We can show that it is indeed hard to factorise large integers, by generating two ten-digit primes and asking Maple to factorise their product: p1 := nextprime(3564965214); p2 := nextprime(3914893877); n := p1*p2; ifactor(n); and then repeating for larger and larger prime pairs.

Question 3
(i) Repeat for 24, 32, 40, 48, 56 and 64-digit pairs of primes. At what point does the factorisation stop being feasible? (ii) Test your primes and your values of n for primality using the Maple function isprime. Does this run into the same kinds of problems? What do you think is the reason for this? Question 3 parts (i) and (ii) carry 5% of the marks. Go to Blackboard and nd the explanation of the RSA encryption algorithm. Now: my 170-digit modulus, n, is 46862651776313668832684618638310007043245135907468247470585960688008 18053474200526957854883187814853515873878978950671057936752518363638 9872513135592162572724499935530721 and my encryption key, e, is 59. (iii) Encrypt your email address using my modulus and encryption key, and send it to me in an email. Youll need to encode it numerically rst (note: you must use the numerical encoding suggested on the RSA handout, or I wont be able to recover your email address). In your email, please send your ciphertext as a large integer; you neednt convert it back into characters. (iv) Send me a public key, in the form of: a modulus of the form n = p1 p2 , where p1 and p2 are prime; the modulus should have between 120 and 200 digits; an encryption key, which can be quite small, but whose HCF with (n) = (p1 1) (p2 1) must be 1. I shall then send you a message, which you should decrypt. When you know what the message is, send me the plaintext in a further email 3

Question 3 parts (iii) and (iv) carry 5% and 10% of the marks respectively. IMPORTANT NOTES: If I can factorise the number you send me, Ive cracked your cipher, and youre a dead spy. Dead spies get zero for Question 3 (iv). This is an iron rule, so be careful; think before you send! If you can factorise my modulus, youve cracked my cipher. Anyone who does this gets an automatic 100% for the whole of this project, no matter how well or badly they do on the rest. (2010s modulus, which was 160 digits long, was successfully factorised by one student.)

## Useful Maple keywords

You may nd some of the following Maple keywords useful, amongst others: proc, if, while, for, mod, iquo, irem, modp, mods, nextprime, rand, ifactor, isprime, time, &^, nops, seq, map. From the StringTools package: Char, Ord, Explode, Implode. From the ListTools package: Search, DotProduct.