Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In python write a function mod_exp(x, y, n) that computes and returns the value

ID: 3920817 • Letter: I

Question

In python write a function mod_exp(x, y, n) that computes and returns the value of x^y mod n. Use the fast modular exponentiation algorithm given in Figure 9.7.4 of the zyBook.

Figure 9.7.4: An iterative algorithm for fast modular exponentiation.

Input: Positive integers x and y.
Output: xy mod n

p := 1     //p holds the partial result.
s := x     //s holds the current x2jx2j.
r := y     //r is used to compute the binary expansion of y

While ( r > 0 )
      If ( r mod 2 = 1 )
            p := p·s mod n
      s := s·s mod n
      r := r div 2
End-while

Return(p)

Explanation / Answer

code:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote