How to resolve the algorithm Modular arithmetic step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Modular arithmetic step by step in the Julia programming language

Table of Contents

Problem Statement

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: The corresponding set of equivalence classes forms a ring denoted

Z

p

Z

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

. When p is a prime number, this ring becomes a field denoted

F

p

{\displaystyle \mathbb {F} _{p}}

, but you won't have to implement the multiplicative inverse for this task. Addition and multiplication on this ring have the same algebraic structure as in usual arithmetic, 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: 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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Modular arithmetic step by step in the Julia programming language

Custom Modular Integer Type:

  • The Modulo struct is a custom data type that represents integers wrapped with a specified modulus.
  • It consists of two fields, val (the actual integer value) and mod (the modulus).
  • The Modulo constructor initializes a new value and modulus.

Conversions:

  • modulo returns a Modulo object from two integers, n and m.
  • Base.convert converts a Modulo object to a specified integer type T.

Display and Copy:

  • The Base.show method overrides the default display behavior for Modulo objects, printing the value and modulus.
  • The Base.copy method creates a shallow copy of a Modulo object.

Arithmetic Operations:

  • The unary + and - operators simply copy or negate the Modulo value.
  • The binary +, -, *, ÷, and ^ operators are overloaded for Modulo objects.
  • If the operands have different moduli, an InexactError is thrown.
  • The result of the operation is wrapped with the same modulus as the first operand.

Example:

  • The f function computes a polynomial expression.
  • The @show macro displays the result of f applied to the Modulo object modulo(10, 13), which represents the integer 10 modulo 13. The result is displayed as "10 (mod 13)".

Source code in the julia programming language

struct Modulo{T<:Integer} <: Integer
    val::T
    mod::T
    Modulo(n::T, m::T) where T = new{T}(mod(n, m), m)
end
modulo(n::Integer, m::Integer) = Modulo(promote(n, m)...)

Base.show(io::IO, md::Modulo) = print(io, md.val, " (mod $(md.mod))")
Base.convert(::Type{T}, md::Modulo) where T<:Integer = convert(T, md.val)
Base.copy(md::Modulo{T}) where T = Modulo{T}(md.val, md.mod)

Base.:+(md::Modulo) = copy(md)
Base.:-(md::Modulo) = Modulo(md.mod - md.val, md.mod)
for op in (:+, :-, :*, :÷, :^)
    @eval function Base.$op(a::Modulo, b::Integer)
        val = $op(a.val, b)
        return Modulo(mod(val, a.mod), a.mod)
    end
    @eval Base.$op(a::Integer, b::Modulo) = $op(b, a)
    @eval function Base.$op(a::Modulo, b::Modulo)
        if a.mod != b.mod throw(InexactError()) end
        val = $op(a.val, b.val)
        return Modulo(mod(val, a.mod), a.mod)
    end
end

f(x) = x ^ 100 + x + 1
@show f(modulo(10, 13))


  

You may also check:How to resolve the algorithm Terminal control/Hiding the cursor step by step in the Perl programming language
You may also check:How to resolve the algorithm Optional parameters step by step in the E programming language
You may also check:How to resolve the algorithm 24 game step by step in the HicEst programming language
You may also check:How to resolve the algorithm Summarize and say sequence step by step in the jq programming language
You may also check:How to resolve the algorithm QR decomposition step by step in the Axiom programming language