Modular arithmetic is a form of arithmetic (a calculation technique involving the concepts of addition and multiplication) which is done on numbers with a defined equivalence relation called congruence.

For any positive integer 



p


{\displaystyle p}

 called the congruence modulus,
two numbers 



a


{\displaystyle a}

 and 



b


{\displaystyle b}

 are said to be congruent modulo p whenever there exists an integer 



k


{\displaystyle k}

 such that:





a
=
b
+
k

p


{\displaystyle a=b+k\,p}


The corresponding set of equivalence classes forms a ring denoted 






Z


p

Z






{\displaystyle {\frac {\mathbb {Z} }{p\mathbb {Z} }}}

.

Addition and multiplication on this ring have the same algebraic structure as in usual arithmetics, so that a function such as a polynomial expression could receive a ring element as argument and give a consistent result.

The purpose of this task is to show, if your programming language allows it,
how to redefine operators so that they can be used transparently on modular integers.
You can do it either by using a dedicated library, or by implementing your own class.

You will use the following function for demonstration:





f
(
x
)
=

x

100


+
x
+
1


{\displaystyle f(x)=x^{100}+x+1}


You will use 



13


{\displaystyle 13}

 as the congruence modulus and you will compute 



f
(
10
)


{\displaystyle f(10)}

.

It is important that the function 



f


{\displaystyle f}

 is agnostic about whether or not its argument is modular; it should behave the same way with normal and modular integers.
In other words, the function is an algebraic expression that could be used with any ring, not just integers.




type mi(object m)
    return sequence(m) and length(m)=2 and integer(m[1]) and integer(m[2])
end type
 
type mii(object m)
    return mi(m) or atom(m)
end type
 
function mi_one(mii a)
    if atom(a) then a=1 else a = {1,a[2]} end if
    return a
end function
 
function mi_add(mii a, mii b)
    if atom(a) then
        if not atom(b) then throw("error") end if
        return a+b
    end if
    if a[2]!=b[2] then throw("error") end if
    a[1] = mod(a[1]+b[1],a[2])
    return a
end function
 
function mi_mul(mii a, mii b)
    if atom(a) then
        if not atom(b) then throw("error") end if
        return a*b
    end if
    if a[2]!=b[2] then throw("error") end if
    a[1] = mod(a[1]*b[1],a[2])
    return a
end function
 
function mi_power(mii x, integer p)
    mii res = mi_one(x)
    for i=1 to p do
        res = mi_mul(res,x)
    end for
    return res
end function
 
function mi_print(mii m)
    return sprintf(iff(atom(m)?"%g":"modint(%d,%d)"),m)
end function
 
function f(mii x)
    return mi_add(mi_power(x,100),mi_add(x,mi_one(x)))
end function
 
procedure test(mii x)
    printf(1,"x^100 + x + 1 for x == %s is %s\n",{mi_print(x),mi_print(f(x))})
end procedure
test(10)
test({10,13})
