How to resolve the algorithm Modular inverse step by step in the MAD programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Modular inverse step by step in the MAD programming language

Table of Contents

Problem Statement

From Wikipedia: In modular arithmetic,   the modular multiplicative inverse of an integer   a   modulo   m   is an integer   x   such that Or in other words, such that: It can be shown that such an inverse exists   if and only if   a   and   m   are coprime,   but we will ignore this for this task.

Either by implementing the algorithm, by using a dedicated library or by using a built-in function in your language,   compute the modular inverse of   42 modulo 2017.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Modular inverse step by step in the MAD programming language

Source code in the mad programming language

            NORMAL MODE IS INTEGER
            INTERNAL FUNCTION(AA, BB)
            ENTRY TO MULINV.
            A = AA
            B = BB 
            WHENEVER B.L.0, B = -B
            WHENEVER A.L.0, A = B - (-(A-A/B*B))
            T = 0
            NT = 1
            R = B
            NR = A-A/B*B
LOOP        WHENEVER NR.NE.0
                Q = R/NR
                TMP = NT
                NT = T - Q*NT
                T = TMP
                TMP = NR
                NR = R - Q*NR
                R = TMP
                TRANSFER TO LOOP
            END OF CONDITIONAL
            WHENEVER R.G.1, FUNCTION RETURN -1
            WHENEVER T.L.0, T = T+B
            FUNCTION RETURN T
            END OF FUNCTION
            
            INTERNAL FUNCTION(AA, BB)
            VECTOR VALUES FMT = $I5,2H, ,I5,2H: ,I5*$
            ENTRY TO SHOW.
            PRINT FORMAT FMT, AA, BB, MULINV.(AA, BB)
            END OF FUNCTION
            
            SHOW.(42,2017)
            SHOW.(40,1)
            SHOW.(52,-217)
            SHOW.(-486,217)
            SHOW.(40,2018)
            END OF PROGRAM

  

You may also check:How to resolve the algorithm Flatten a list step by step in the Go programming language
You may also check:How to resolve the algorithm Magic constant step by step in the AWK programming language
You may also check:How to resolve the algorithm Sort an outline at every level step by step in the Nim programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Function definition step by step in the SenseTalk programming language