{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

modularinversion

# modularinversion - 10 = 4*2 2 4 = 2*2 0 Modular Inversion...

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

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:

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

View Full Document
> 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
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

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern