Modular arithmetic: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Cleanup typo)
m (Tinker with the description)
Line 1: Line 1:
{{draft task}}
{{draft task}}


'''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 ''congruence''. This means that two numbers a and b are considered congruent whenever there exists an integer k such that:
'''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 ''congruence''. This means that two numbers <math>a</math> and <math>b</math> are considered congruent whenever there exists an integer <math>k</math> such that:


<math>a = b + k p</math>
<math>a = b + k p</math>


p is called the congruence ''modulus''. The corresponding set of integers is called the <math>\frac{\mathbf{Z}}{p\mathbf{Z}}</math> [[wp:Ring (mathematics)|ring]], where each element is uniquely represented by the remainder of its [[wp:euclidean division|euclidean division]] by p. Addition and multiplication on this ring have the same algebraic structure, so that a function such as a polynomial expression could receive a ring element as argument and give a consistent result.
<math>p</math> is called the congruence ''modulus''. The corresponding set of integers is called the <math>\frac{\mathbf{Z}}{p\mathbf{Z}}</math> [[wp:Ring (mathematics)|ring]], where each element is uniquely represented by the remainder of its [[wp:euclidean division|euclidean division]] by <math>p</math>. Addition and multiplication on this ring have the same algebraic structure, 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.
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.
Line 13: Line 13:
<math>f(x) = x^{100} + x + 1</math>
<math>f(x) = x^{100} + x + 1</math>


You will use 13 as the congruence modulus and you will compute f(10).
You will use <math>13</math> as the congruence modulus and you will compute <math>f(10)</math>.


It is important that the function 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.
It is important that the function <math>f</math> 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.


=={{header|Perl}}==
=={{header|Perl}}==

Revision as of 10:16, 9 June 2013

Modular arithmetic is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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 congruence. This means that two numbers and are considered congruent whenever there exists an integer such that:

is called the congruence modulus. The corresponding set of integers is called the ring, where each element is uniquely represented by the remainder of its euclidean division by . Addition and multiplication on this ring have the same algebraic structure, 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 as the congruence modulus and you will compute .

It is important that the function 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.

Perl

There is a CPAN module called Math::ModInt which does the job.

<lang Perl>use Math::ModInt qw(mod); sub f { my $x = shift; $x**100 + $x + 1 }; print f mod(10, 13);</lang>

Output:
mod(1, 13)

Perl 6

There is a Panda module called Modular which works basically as Perl 5's Math::ModInt.

<lang Perl 6>use Modular; sub f(\x) { x**100 + x + 1}; say f( 10 Mod 13 )</lang>

Output:
1 「mod 13」

Racket

<lang racket>

  1. lang racket

(require racket/require

        ;; grab all "mod*" names, but get them without the "mod", so
        ;; `+' and `expt' is actually `mod+' and `modexpt'
        (filtered-in (λ(n) (and (regexp-match? #rx"^mod" n)
                                (regexp-replace #rx"^mod" n "")))
                     math)
        (only-in math with-modulus))

(define (f x) (+ (expt x 100) x 1)) (with-modulus 13 (f 10))

=> 1

</lang>