How to resolve the algorithm Modular arithmetic step by step in the Ada programming language
How to resolve the algorithm Modular arithmetic step by step in the Ada 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 Ada programming language
Source code in the ada programming language
with Ada.Text_IO;
procedure Modular_Demo is
type Modul_13 is mod 13;
function F (X : Modul_13) return Modul_13 is
begin
return X**100 + X + 1;
end F;
package Modul_13_IO is
new Ada.Text_IO.Modular_IO (Modul_13);
use Ada.Text_IO;
use Modul_13_IO;
X_Integer : constant Integer := 10;
X_Modul_13 : constant Modul_13 := Modul_13'Mod (X_Integer);
F_10 : constant Modul_13 := F (X_Modul_13);
begin
Put ("f("); Put (X_Modul_13); Put (" mod "); Put (Modul_13'Modulus'Image); Put (") = ");
Put (F_10); Put (" mod "); Put (Modul_13'Modulus'Image);
New_Line;
end Modular_Demo;
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the Nim programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Guess the number step by step in the RapidQ programming language
You may also check:How to resolve the algorithm Find the missing permutation step by step in the Ring programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the ooRexx programming language