modularinversion - 10 = 4*2 + 2 4 = 2*2 + 0 Modular...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Modular Inversion Euclid's Algorithm PSEUDO COMPUTER CODE Input a and b while a>0 do t <--- b b <--- a a <--- t - a*[t/a] Output b STOP MAPLE CODE (ATTEMPT 1) > euclid:=proc(a,b) > local t: > if a=0 and b=0 then > ERROR("gcd(a,b) is not defined if a=b=0"); > fi: > > > while a>0 do > t:=b: > b:=a: > a:=t - a*trunc(t/a): > od: > RETURN(b): > end: > euclid(62,14222); Error, (in euclid) illegal use of a formal parameter This proc is returning an ERROR because in a MAPLE you can not change the value of input variables. We can fix this problem by using some additional local variables. MAPLE CODE (FINAL VERSION) > euclid:=proc(a,b) > local A,B,t: > if a=0 and b=0 then > ERROR("gcd(a,b) is not defined if a=b=0"); > fi: > A:=a: > B:=b: > while A>0 do > t:=B:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
> B:=A: > A:=t - A*trunc(t/A):print(A); > od: > RETURN(B): > end: > euclid(0,0); > > euclid(62,14222); 14222 = 62*232 + 38 62 = 38*1 + 24 38= 24*1 + 14 24= 14*1 + 10 14= 10*1 + 4
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 10 = 4*2 + 2 4 = 2*2 + 0 Modular Inversion Algorithm Pseudocode Input r and n Let s'=0 and s=1 while r>0 do t <--- n n <--- r q <--- [t/n] r <--- t - r*q u <--- s s <-- s' - q*s s' <--- u Output s' STOP EXERCISE (10 points) Write a MAPLE implementation of the Modular Inversion Algorithm. Call your function (or proc) modinv. Include an error statement to test whether r and n are relatively prime. Your function should take the form > modinv := proc(r,n) > > > > > > > > > > > > > > > This should compute the multiplicative inverse of r mod n. Test your function with an example: > modinv(23,171); > Did you get 119? This can be checked using MAPLE's modp function. > modp(1/23,171); 119 > (23*119-1)/171; 16 Note: That 119 is the inverse of 23 mod 171 since 119*23=16*171 + 1. Test your function with at least one more example. > > > >...
View Full Document

This note was uploaded on 06/04/2011 for the course MAS 4202 taught by Professor Boyland during the Spring '10 term at University of Florida.

Page1 / 3

modularinversion - 10 = 4*2 + 2 4 = 2*2 + 0 Modular...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online