Modular arithmetic: Difference between revisions
Adding the Forth solution |
No edit summary |
||
Line 557: | Line 557: | ||
MI{ 3 7 } 50 ^ = MI{ 2 7 } |
MI{ 3 7 } 50 ^ = MI{ 2 7 } |
||
</pre> |
</pre> |
||
=={{header|Forth}} |
=={{header|Forth}}== |
||
<pre> |
|||
\ We would normally define operators that have a suffix `m' in order |
\ We would normally define operators that have a suffix `m' in order |
||
\ not be confused: +m -m *m /m **m |
\ not be confused: +m -m *m /m **m |
||
Line 607: | Line 608: | ||
10 DUP 100 ** + 1 + . CR |
10 DUP 100 ** + 1 + . CR |
||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Go does not allow redefinition of operators. That element of the task cannot be done in Go. The element of defining f so that it can be used with any ring however can be done, just not with the syntactic sugar of operator redefinition. |
Go does not allow redefinition of operators. That element of the task cannot be done in Go. The element of defining f so that it can be used with any ring however can be done, just not with the syntactic sugar of operator redefinition. |
Revision as of 13:17, 27 April 2022
You are encouraged to solve this task according to the task description, using any language you may know.
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 called the congruence modulus, two numbers and are said to be congruent modulo p whenever there exists an integer such that:
The corresponding set of equivalence classes forms a ring denoted .
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:
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.
Ada
Ada has modular types. <lang Ada>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;</lang>
- Output:
f( 10 mod 13) = 1 mod 13
ALGOL 68
<lang algol68># allow for large integers in Algol 68G # PR precision 200 PR
- modular integer type #
MODE MODULARINT = STRUCT( LONG LONG INT v, INT modulus );
- modular integer + and * operators #
- where both operands are modular, they must have the same modulus #
OP + = ( MODULARINT a, b )MODULARINT: ( ( v OF a + v OF b ) MOD modulus OF a, modulus OF a ); OP + = ( MODULARINT a, INT b )MODULARINT: ( ( v OF a + b ) MOD modulus OF a, modulus OF a ); OP * = ( MODULARINT a, b )MODULARINT: ( ( v OF a * v OF b ) MOD modulus OF a, modulus OF a ); OP ** = ( MODULARINT a, INT b )MODULARINT: ( ( v OF a ** b ) MOD modulus OF a, modulus OF a );
- f(x) function - can be applied to either LONG LONG INT or MODULARINT values #
- the result is always a LONG LONG INT #
PROC f = ( UNION( LONG LONG INT, MODULARINT ) x )LONG LONG INT:
CASE x IN ( LONG LONG INT ix ): ( ix**100 + ix + 1 ) , ( MODULARINT mx ): v OF ( mx**100 + mx + 1 ) ESAC;
print( ( whole( f( MODULARINT( 10, 13 ) ), 0 ), newline ) )</lang>
- Output:
1
C
<lang C>#include <stdio.h>
struct ModularArithmetic {
int value; int modulus;
};
struct ModularArithmetic make(const int value, const int modulus) {
struct ModularArithmetic r = { value % modulus, modulus }; return r;
}
struct ModularArithmetic add(const struct ModularArithmetic a, const struct ModularArithmetic b) {
return make(a.value + b.value, a.modulus);
}
struct ModularArithmetic addi(const struct ModularArithmetic a, const int v) {
return make(a.value + v, a.modulus);
}
struct ModularArithmetic mul(const struct ModularArithmetic a, const struct ModularArithmetic b) {
return make(a.value * b.value, a.modulus);
}
struct ModularArithmetic pow(const struct ModularArithmetic b, int pow) {
struct ModularArithmetic r = make(1, b.modulus); while (pow-- > 0) { r = mul(r, b); } return r;
}
void print(const struct ModularArithmetic v) {
printf("ModularArithmetic(%d, %d)", v.value, v.modulus);
}
struct ModularArithmetic f(const struct ModularArithmetic x) {
return addi(add(pow(x, 100), x), 1);
}
int main() {
struct ModularArithmetic input = make(10, 13); struct ModularArithmetic output = f(input);
printf("f("); print(input); printf(") = "); print(output); printf("\n");
return 0;
}</lang>
- Output:
f(ModularInteger(10, 13)) = ModularInteger(1, 13)
C#
<lang csharp>using System;
namespace ModularArithmetic {
interface IAddition<T> { T Add(T rhs); } interface IMultiplication<T> { T Multiply(T rhs); } interface IPower<T> { T Power(int pow); } interface IOne<T> { T One(); }
class ModInt : IAddition<ModInt>, IMultiplication<ModInt>, IPower<ModInt>, IOne<ModInt> { private int modulo;
public ModInt(int value, int modulo) { Value = value; this.modulo = modulo; }
public int Value { get; }
public ModInt One() { return new ModInt(1, modulo); }
public ModInt Add(ModInt rhs) { return this + rhs; }
public ModInt Multiply(ModInt rhs) { return this * rhs; }
public ModInt Power(int pow) { return Pow(this, pow); }
public override string ToString() { return string.Format("ModInt({0}, {1})", Value, modulo); }
public static ModInt operator +(ModInt lhs, ModInt rhs) { if (lhs.modulo != rhs.modulo) { throw new ArgumentException("Cannot add rings with different modulus"); } return new ModInt((lhs.Value + rhs.Value) % lhs.modulo, lhs.modulo); }
public static ModInt operator *(ModInt lhs, ModInt rhs) { if (lhs.modulo != rhs.modulo) { throw new ArgumentException("Cannot add rings with different modulus"); } return new ModInt((lhs.Value * rhs.Value) % lhs.modulo, lhs.modulo); }
public static ModInt Pow(ModInt self, int p) { if (p < 0) { throw new ArgumentException("p must be zero or greater"); }
int pp = p; ModInt pwr = self.One(); while (pp-- > 0) { pwr *= self; } return pwr; } }
class Program { static T F<T>(T x) where T : IAddition<T>, IMultiplication<T>, IPower<T>, IOne<T> { return x.Power(100).Add(x).Add(x.One()); }
static void Main(string[] args) { ModInt x = new ModInt(10, 13); ModInt y = F(x); Console.WriteLine("x ^ 100 + x + 1 for x = {0} is {1}", x, y); } }
}</lang>
- Output:
x ^ 100 + x + 1 for x = ModInt(10, 13) is ModInt(1, 13)
C++
<lang cpp>#include <iostream>
- include <ostream>
template<typename T> T f(const T& x) {
return (T) pow(x, 100) + x + 1;
}
class ModularInteger { private:
int value; int modulus;
void validateOp(const ModularInteger& rhs) const { if (modulus != rhs.modulus) { throw std::runtime_error("Left-hand modulus does not match right-hand modulus."); } }
public:
ModularInteger(int v, int m) { modulus = m; value = v % m; }
int getValue() const { return value; }
int getModulus() const { return modulus; }
ModularInteger operator+(const ModularInteger& rhs) const { validateOp(rhs); return ModularInteger(value + rhs.value, modulus); }
ModularInteger operator+(int rhs) const { return ModularInteger(value + rhs, modulus); }
ModularInteger operator*(const ModularInteger& rhs) const { validateOp(rhs); return ModularInteger(value * rhs.value, modulus); }
friend std::ostream& operator<<(std::ostream&, const ModularInteger&);
};
std::ostream& operator<<(std::ostream& os, const ModularInteger& self) {
return os << "ModularInteger(" << self.value << ", " << self.modulus << ")";
}
ModularInteger pow(const ModularInteger& lhs, int pow) {
if (pow < 0) { throw std::runtime_error("Power must not be negative."); }
ModularInteger base(1, lhs.getModulus()); while (pow-- > 0) { base = base * lhs; } return base;
}
int main() {
using namespace std;
ModularInteger input(10, 13); auto output = f(input); cout << "f(" << input << ") = " << output << endl;
return 0;
}</lang>
- Output:
f(ModularInteger(10, 13)) = ModularInteger(1, 13)
D
<lang D>import std.stdio;
version(unittest) {
void assertEquals(T)(T actual, T expected) { import core.exception; import std.conv; if (actual != expected) { throw new AssertError("Actual [" ~ to!string(actual) ~ "]; Expected [" ~ to!string(expected) ~ "]"); } }
}
void main() {
auto input = ModularInteger(10,13); auto output = f(input); writeln("f(", input, ") = ", output);
}
V f(V)(const V x) {
return x^^100 + x + 1;
}
/// Integer tests on f unittest {
assertEquals(f(1), 3); assertEquals(f(0), 1);
}
/// Floating tests on f unittest {
assertEquals(f(1.0), 3.0); assertEquals(f(0.0), 1.0);
}
struct ModularInteger {
private: int value; int modulus;
public: this(int value, int modulus) { this.modulus = modulus; this.value = value % modulus; }
ModularInteger opBinary(string op : "+")(ModularInteger rhs) const in { assert(this.modulus == rhs.modulus); } body { return ModularInteger((this.value + rhs.value) % this.modulus, this.modulus); }
ModularInteger opBinary(string op : "+")(int rhs) const { return ModularInteger((this.value + rhs) % this.modulus, this.modulus); }
ModularInteger opBinary(string op : "*")(ModularInteger rhs) const in { assert(this.modulus == rhs.modulus); assert(this.value < this.modulus); assert(rhs.value < this.modulus); } body { return ModularInteger((this.value * rhs.value) % this.modulus, this.modulus); }
ModularInteger opBinary(string op : "^^")(int pow) const in { assert(pow >= 0); } body { auto base = ModularInteger(1, this.modulus); while (pow-- > 0) { base = base * this; } return base; }
string toString() { import std.format; return format("ModularInteger(%s, %s)", value, modulus); }
}
/// Addition with same type of int unittest {
auto a = ModularInteger(2,5); auto b = ModularInteger(3,5); assertEquals(a+b, ModularInteger(0,5));
}
/// Addition with differnt int types unittest {
auto a = ModularInteger(2,5); assertEquals(a+0, a); assertEquals(a+1, ModularInteger(3,5));
}
/// Muliplication unittest {
auto a = ModularInteger(2,5); auto b = ModularInteger(3,5); assertEquals(a*b, ModularInteger(1,5));
}
/// Power unittest {
const a = ModularInteger(3,13); assertEquals(a^^2, ModularInteger(9,13)); assertEquals(a^^3, ModularInteger(1,13));
const b = ModularInteger(10,13); assertEquals(b^^1, ModularInteger(10,13)); assertEquals(b^^2, ModularInteger(9,13)); assertEquals(b^^3, ModularInteger(12,13)); assertEquals(b^^4, ModularInteger(3,13)); assertEquals(b^^5, ModularInteger(4,13)); assertEquals(b^^6, ModularInteger(1,13)); assertEquals(b^^7, ModularInteger(10,13)); assertEquals(b^^8, ModularInteger(9,13)); assertEquals(b^^10, ModularInteger(3,13)); assertEquals(b^^20, ModularInteger(9,13)); assertEquals(b^^30, ModularInteger(1,13)); assertEquals(b^^50, ModularInteger(9,13)); assertEquals(b^^75, ModularInteger(12,13)); assertEquals(b^^90, ModularInteger(1,13)); assertEquals(b^^95, ModularInteger(4,13)); assertEquals(b^^97, ModularInteger(10,13)); assertEquals(b^^98, ModularInteger(9,13)); assertEquals(b^^99, ModularInteger(12,13)); assertEquals(b^^100, ModularInteger(3,13));
}</lang>
- Output:
f(ModularInteger(10, 13)) = ModularInteger(1, 13)
Factor
While it's probably not the best idea to define methods in arithmetic words that specialize on custom classes, it can be done. There are a few pitfalls to doing so, which is why custom types typically implement their own arithmetic words. Examples are words like v+
from the math.vectors
vocabulary and q+
from the math.quaternions
vocabulary.
The pitfalls are as follows:
First, arithmetic words are declared using MATH:
, which means they use the math method combination. These methods will dispatch on both their arguments, and promote lower-priority numeric types to higher-priority types when both types are different. The math method combination also means that methods added to MATH:
words cannot specialize on any classes except for fixnum
,
bignum
, ratio
, float
, complex
, object
, or unions of them.
This is a bit of a problem, because we must specialize on object
and then do a bunch of manual type checking and stack shuffling to make sure we are performing the correct operations on the correct objects.
Second, if any other vocabularies add methods that specialize on arithmetic words, they will conflict with our modular arithmetic vocabulary due to the aforementioned inability to specialize on specific classes.
For these reasons, I would normally opt to define my own arithmetic words, with the added bonus of being able to use non-MATH:
multiple dispatch (from the multi-methods
vocabulary) to cleanly implement mixed-type dispatch.
Also note that since ^
is not a generic word, we employ the strategy of renaming it to **
inside our vocabulary and defining a new word named ^
that can also handle modular integers. This is an acceptable way to handle it because Factor has pretty good word-disambiguation faculties. I just wouldn't want to have to employ them for more frequently-used arithmetic.
<lang factor>USING: accessors generalizations io kernel math math.functions parser prettyprint prettyprint.custom sequences ; IN: rosetta-code.modular-arithmetic RENAME: ^ math.functions => **
! Define a modular integer class. TUPLE: mod-int
{ n integer read-only } { mod integer read-only } ;
! Define a constructor for mod-int. C: <mod-int> mod-int
ERROR: non-equal-modulus m1 m2 ;
! Define a literal syntax for mod-int. << SYNTAX: MI{ \ } [ first2 <mod-int> ] parse-literal ; >>
! Implement prettyprinting for mod-int custom syntax. M: mod-int pprint-delims drop \ MI{ \ } ; M: mod-int >pprint-sequence [ n>> ] [ mod>> ] bi { } 2sequence ; M: mod-int pprint* pprint-object ;
<PRIVATE
! Helper words for displaying the results of an arithmetic ! operation.
- show ( quot -- )
[ unparse 2 tail but-last "= " append write ] [ call . ] bi ; inline
- 2show ( quots -- )
[ 2curry show ] map-compose [ call( -- ) ] each ; inline
! Check whether two mod-ints have the same modulus and throw an ! error if not.
- check-mod ( m1 m2 -- )
2dup [ mod>> ] bi@ = [ 2drop ] [ non-equal-modulus ] if ;
! Apply quot to the integer parts of two mod-ints and create a ! new mod-int from the result.
- mod-int-op ( m1 m2 quot -- m3 )
[ [ n>> ] bi@ ] prepose [ 2dup check-mod ] dip over mod>> [ call( x x -- x ) ] dip [ mod ] keep <mod-int> ; inline
! Promote an integer to a mod-int and call mod-int-op.
- integer-op ( obj1 obj2 quot -- mod-int )
[ dup integer? [ over mod>> <mod-int> ] [ dup [ mod>> <mod-int> ] dip ] if ] dip mod-int-op ; inline
! Apply quot, a binary function, to any combination of integers ! and mod-ints.
- binary-op ( obj1 obj2 quot -- mod-int )
2over [ mod-int? ] both? [ mod-int-op ] [ integer-op ] if ; inline
PRIVATE>
! This is where the arithmetic words are 'redefined' by adding a ! method to them that specializes on the object class. M: object + [ + ] binary-op ; M: object - [ - ] binary-op ; M: object * [ * ] binary-op ; M: object /i [ /i ] binary-op ;
! ^ is a special case because it is not generic.
- ^ ( obj1 obj2 -- obj3 )
2dup [ mod-int? ] either? [ [ ** ] binary-op ] [ ** ] if ;
- fn ( obj -- obj' ) dup 100 ^ + 1 + ;
- modular-arithmetic-demo ( -- )
[ MI{ 10 13 } fn ] [ 2 fn ] [ show ] bi@ { [ MI{ 10 13 } MI{ 5 13 } [ + ] ] [ MI{ 10 13 } 5 [ + ] ] [ 5 MI{ 10 13 } [ + ] ] [ MI{ 10 13 } 2 [ /i ] ] [ 5 10 [ * ] ] [ MI{ 3 7 } MI{ 4 7 } [ * ] ] [ MI{ 3 7 } 50 [ ^ ] ] } 2show ;
MAIN: modular-arithmetic-demo</lang>
- Output:
MI{ 10 13 } fn = MI{ 1 13 } 2 fn = 1267650600228229401496703205379 MI{ 10 13 } MI{ 5 13 } + = MI{ 2 13 } MI{ 10 13 } 5 + = MI{ 2 13 } 5 MI{ 10 13 } + = MI{ 2 13 } MI{ 10 13 } 2 /i = MI{ 5 13 } 5 10 * = 50 MI{ 3 7 } MI{ 4 7 } * = MI{ 5 7 } MI{ 3 7 } 50 ^ = MI{ 2 7 }
Forth
\ We would normally define operators that have a suffix `m' in order \ not be confused: +m -m *m /m **m \ Also useful is %:m reduce a number modulo. \ Words that may be not be present in the kernel. \ This example loads them in ciforth. WANT ALIAS VOCABULARY VARIABLE _m ( Modulo number) \ Set the modulus to m . : set-modulus _m ! ; \ For A B return C GCD where C*A+x*B=GCD : XGCD 1 0 2SWAP BEGIN OVER /MOD OVER WHILE >R SWAP 2SWAP OVER R> * - SWAP 2SWAP REPEAT 2DROP NIP ; \ Suffix n : normalized number. : _norm_-m DUP 0< _m @ AND + ; ( x -- xn ) \ -m<xn<+m : +m + _m @ - _norm_-m ; ( an bn -- sumn ) : -m - _norm_-m ; ( an bn -- diffn) : *m M* _m @ SM/REM DROP ; ( an bn -- prodn) : /m _m @ XGCD DROP _norm_-m *m ; ( a b -- quotn) : %:m S>D _m @ SM/REM DROP _norm_-m ; ( a -- an) \ Both steps: For A B and C: return A B en C. Invariant A*B^C. : _reduce_1- 1- >R >R R@ *m R> R> ; : _reduce_2/ 2/ >R DUP *m R> ; : **m 1 ROT ROT BEGIN DUP 1 AND IF _reduce_1- THEN _reduce_2/ DUP 0= UNTIL 2DROP ; ( a b -- apowbn ) \ The solution is 13 set-modulus 10 DUP 100 **m +m 1 +m . CR \ In order to comply with the problem we can generate a separate namespace \ and import the above definitions. VOCABULARY MODULO ALSO MODULO DEFINITIONS ' set-modulus ALIAS set-modulus ' +m ALIAS + ' -m ALIAS - ' *m ALIAS * ' /m ALIAS / ' **m ALIAS ** \ now the calculation becomes 13 set-modulus 10 DUP 100 ** + 1 + . CR
Go
Go does not allow redefinition of operators. That element of the task cannot be done in Go. The element of defining f so that it can be used with any ring however can be done, just not with the syntactic sugar of operator redefinition. <lang go>package main
import "fmt"
// Define enough of a ring to meet the needs of the task. Addition and // multiplication are mentioned in the task; multiplicative identity is not // mentioned but is useful for the power function.
type ring interface {
add(ringElement, ringElement) ringElement mul(ringElement, ringElement) ringElement mulIdent() ringElement
}
type ringElement interface{}
// Define a power function that works for any ring.
func ringPow(r ring, a ringElement, p uint) (pow ringElement) {
for pow = r.mulIdent(); p > 0; p-- { pow = r.mul(pow, a) } return
}
// The task function f has that constant 1 in it. // Define a special kind of ring that has this element.
type oneRing interface {
ring one() ringElement // return ring element corresponding to '1'
}
// Now define the required function f. // It works for any ring (that has a "one.")
func f(r oneRing, x ringElement) ringElement {
return r.add(r.add(ringPow(r, x, 100), x), r.one())
}
// With rings and the function f defined in a general way, now define // the specific ring of integers modulo n.
type modRing uint // value is congruence modulus n
func (m modRing) add(a, b ringElement) ringElement {
return (a.(uint) + b.(uint)) % uint(m)
}
func (m modRing) mul(a, b ringElement) ringElement {
return (a.(uint) * b.(uint)) % uint(m)
}
func (modRing) mulIdent() ringElement { return uint(1) }
func (modRing) one() ringElement { return uint(1) }
// Demonstrate the general function f on the specific ring with the // specific values.
func main() {
fmt.Println(f(modRing(13), uint(10)))
}</lang>
- Output:
1
Haskell
<lang haskell>-- We use a couple of GHC extensions to make the program cooler. They let us -- use / as an operator and 13 as a literal at the type level. (The library -- also provides the fancy Zahlen (ℤ) symbol as a synonym for Integer.)
{-# Language DataKinds #-} {-# Language TypeOperators #-}
import Data.Modular
f :: ℤ/13 -> ℤ/13 f x = x^100 + x + 1
main :: IO () main = print (f 10)</lang>
- Output:
./modarith 1
J
<lang J> f=: (+./1 1,:_101{.1x)&p.</lang>
Task example:
<lang J> 13|f 10 1</lang>
Java
<lang Java>public class ModularArithmetic {
private interface Ring<T> { Ring<T> plus(Ring<T> rhs);
Ring<T> times(Ring<T> rhs);
int value();
Ring<T> one();
default Ring<T> pow(int p) { if (p < 0) { throw new IllegalArgumentException("p must be zero or greater"); }
int pp = p; Ring<T> pwr = this.one(); while (pp-- > 0) { pwr = pwr.times(this); } return pwr; } }
private static class ModInt implements Ring<ModInt> { private int value; private int modulo;
private ModInt(int value, int modulo) { this.value = value; this.modulo = modulo; }
@Override public Ring<ModInt> plus(Ring<ModInt> other) { if (!(other instanceof ModInt)) { throw new IllegalArgumentException("Cannot add an unknown ring."); } ModInt rhs = (ModInt) other; if (modulo != rhs.modulo) { throw new IllegalArgumentException("Cannot add rings with different modulus"); } return new ModInt((value + rhs.value) % modulo, modulo); }
@Override public Ring<ModInt> times(Ring<ModInt> other) { if (!(other instanceof ModInt)) { throw new IllegalArgumentException("Cannot multiple an unknown ring."); } ModInt rhs = (ModInt) other; if (modulo != rhs.modulo) { throw new IllegalArgumentException("Cannot multiply rings with different modulus"); } return new ModInt((value * rhs.value) % modulo, modulo); }
@Override public int value() { return value; }
@Override public Ring<ModInt> one() { return new ModInt(1, modulo); }
@Override public String toString() { return String.format("ModInt(%d, %d)", value, modulo); } }
private static <T> Ring<T> f(Ring<T> x) { return x.pow(100).plus(x).plus(x.one()); }
public static void main(String[] args) { ModInt x = new ModInt(10, 13); Ring<ModInt> y = f(x); System.out.print("x ^ 100 + x + 1 for x = ModInt(10, 13) is "); System.out.println(y); System.out.flush(); }
}</lang>
- Output:
x ^ 100 + x + 1 for x = ModInt(10, 13) is ModInt(1, 13)
jq
Works with gojq, the Go implementation of jq
This entry focuses on the requirement that the function, f, "should behave the same way with normal and modular integers."
To illustrate that the function `ring::f` defined here does satisfy this requirement, we will evaluate it at both the integer 1 and the element «10 % 13» of ℤ/13ℤ.
To keep the distinctions between functions defined on different types clear, this entry uses jq's support for name spaces. Specifically, we will use the prefix "modint::" for the modular arithmetic functions, and "ring::" for the generic ring functions.
Since jq supports neither redefining any of the symbolic operators (such as "+") nor dynamic dispatch, ring functions (such as `ring::add`) must be written with the specific rings that are to be supported in mind.
Preliminaries <lang jq>def assert($e; $msg): if $e then . else "assertion violation @ \($msg)" | error end;
def is_integer: type=="number" and floor == .;
- To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); </lang> Modular Arithmetic <lang jq># "ModularArithmetic" objects are represented by JSON objects of the form: {value, mod}.
- The function modint::assert/0 checks the input is of this form with integer values.
def is_modint: type=="object" and has("value") and has("mod");
def modint::assert:
assert(type=="object"; "object expected") | assert(has("value"); "object should have a value") | assert(has("mod"); "object should have a mod") | assert(.value | is_integer; "value should be an integer") | assert(.mod | is_integer; "mod should be an integer");
def modint::make($value; $mod):
assert($value|is_integer; "value should be an integer") | assert($mod|is_integer; "mod should be an integer") | { value: ($value % $mod), mod: $mod};
def modint::add($A; $B):
if ($B|type) == "object" then assert($A.mod == $B.mod ; "modint::add") | modint::make( $A.value + $B.value; $A.mod ) else modint::make( $A.value + $B; $A.mod ) end;
def modint::mul($A; $B):
if ($B|type) == "object" then assert($A.mod == $B.mod ; "mul") | modint::make( $A.value * $B.value; $A.mod ) else modint::make( $A.value * $B; $A.mod ) end;
def modint::pow($A; $pow):
assert($pow | is_integer; "pow") | reduce range(0; $pow) as $i ( modint::make(1; $A.mod); modint::mul( .; $A) );
- pretty print
def modint::pp: "«\(.value) % \(.mod)»";</lang> Ring Functions <lang jq>def ring::add($A; $B):
if $A|is_modint then modint::add($A; $B) elif $A|is_integer then $A + $B else "ring::add" | error end;
def ring::mul($A; $B):
if $A|is_modint then modint::mul($A; $B) elif $A|is_integer then $A * $B else "ring::mul" | error end;
def ring::pow($A; $B):
if $A|is_modint then modint::pow($A; $B) elif $A|is_integer then $A|power($B) else "ring::pow" | error end;
def ring::pp:
if is_modint then modint::pp elif is_integer then . else "ring::pp" | error end;
def ring::f($x):
ring::add( ring::add( ring::pow($x; 100); $x); 1);</lang>
Evaluating ring::f <lang jq>def main:
(ring::f(1) | "f(\(1)) => \(.)"), (modint::make(10;13) | ring::f(.) as $out | "f(\(ring::pp)) => \($out|ring::pp)");</lang>
- Output:
f(1) => 3 f(«10 % 13») => «1 % 13»
Julia
Implements the Modulo struct and basic operations. <lang julia>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))</lang>
- Output:
f(modulo(10, 13)) = 11 (mod 13)
Kotlin
<lang scala>// version 1.1.3
interface Ring<T> {
operator fun plus(other: Ring<T>): Ring<T> operator fun times(other: Ring<T>): Ring<T> val value: Int val one: Ring<T>
}
fun <T> Ring<T>.pow(p: Int): Ring<T> {
require(p >= 0) var pp = p var pwr = this.one while (pp-- > 0) pwr *= this return pwr
}
class ModInt(override val value: Int, val modulo: Int): Ring<ModInt> {
override operator fun plus(other: Ring<ModInt>): ModInt { require(other is ModInt && modulo == other.modulo) return ModInt((value + other.value) % modulo, modulo) } override operator fun times(other: Ring<ModInt>): ModInt { require(other is ModInt && modulo == other.modulo) return ModInt((value * other.value) % modulo, modulo) }
override val one get() = ModInt(1, modulo)
override fun toString() = "ModInt($value, $modulo)"
}
fun <T> f(x: Ring<T>): Ring<T> = x.pow(100) + x + x.one
fun main(args: Array<String>) {
val x = ModInt(10, 13) val y = f(x) println("x ^ 100 + x + 1 for x == ModInt(10, 13) is $y")
}</lang>
- Output:
x ^ 100 + x + 1 for x == ModInt(10, 13) is ModInt(1, 13)
Lua
<lang lua>function make(value, modulo)
local v = value % modulo local tbl = {value=v, modulo=modulo}
local mt = { __add = function(lhs, rhs) if type(lhs) == "table" then if type(rhs) == "table" then if lhs.modulo ~= rhs.modulo then error("Cannot add rings with different modulus") end return make(lhs.value + rhs.value, lhs.modulo) else return make(lhs.value + rhs, lhs.modulo) end else error("lhs is not a table in +") end end, __mul = function(lhs, rhs) if lhs.modulo ~= rhs.modulo then error("Cannot multiply rings with different modulus") end return make(lhs.value * rhs.value, lhs.modulo) end, __pow = function(b,p) if p<0 then error("p must be zero or greater") end
local pp = p local pwr = make(1, b.modulo) while pp > 0 do pp = pp - 1 pwr = pwr * b end return pwr end, __concat = function(lhs, rhs) if type(lhs) == "table" and type(rhs) == "string" then return "ModInt("..lhs.value..", "..lhs.modulo..")"..rhs elseif type(lhs) == "string" and type(rhs) == "table" then return lhs.."ModInt("..rhs.value..", "..rhs.modulo..")" else return "todo" end end }
setmetatable(tbl, mt) return tbl
end
function func(x)
return x ^ 100 + x + 1
end
-- main local x = make(10, 13) local y = func(x) print("x ^ 100 + x + 1 for "..x.." is "..y)</lang>
- Output:
x ^ 100 + x + 1 for ModInt(10, 13) is ModInt(1, 13)
Mathematica /Wolfram Language
The best way to do it is probably to use the finite fields package. <lang Mathematica><< FiniteFields` x^100 + x + 1 /. x -> GF[13]@{10}</lang>
- Output:
{1}13
Nim
Modular integers are represented as distinct integers with a modulus N managed by the compiler. <lang Nim>import macros, sequtils, strformat, strutils
const Subscripts: array['0'..'9', string] = ["₀", "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉"]
- Modular integer with modulus N.
type ModInt[N: static int] = distinct int
- ---------------------------------------------------------------------------------------------------
- Creation.
func initModInt[N](n: int): ModInt[N] =
## Create a modular integer from an integer. static: when N < 2: error "Modulus must be greater than 1." if n >= N: raise newException(ValueError, &"value must be in 0..{N - 1}.") result = ModInt[N](n)
- ---------------------------------------------------------------------------------------------------
- Arithmetic operations: ModInt op ModInt, ModInt op int and int op ModInt.
func `+`*[N](a, b: ModInt[N]): ModInt[N] =
ModInt[N]((a.int + b.int) mod N)
func `+`*[N](a: ModInt[N]; b: int): ModInt[N] =
a + initModInt[N](b)
func `+`*[N](a: int; b: ModInt[N]): ModInt[N] =
initModInt[N](a) + b
func `*`*[N](a, b: ModInt[N]): ModInt[N] =
ModInt[N]((a.int * b.int) mod N)
func `*`*[N](a: ModInt[N]; b: int): ModInt[N] =
a * initModInt[N](b)
func `*`*[N](a: int; b: ModInt[N]): ModInt[N] =
initModInt[N](a) * b
func `^`*[N](a: ModInt[N]; n: Natural): ModInt[N] =
var a = a var n = n result = initModInt[N](1) while n > 0: if (n and 1) != 0: result = result * a n = n shr 1 a = a * a
- ---------------------------------------------------------------------------------------------------
- Representation of a modular integer as a string.
template subscript(n: Natural): string =
mapIt($n, Subscripts[it]).join()
func `$`(a: ModInt): string =
&"{a.int}{subscript(a.N)})"
- ---------------------------------------------------------------------------------------------------
- The function "f" defined for any modular integer, the same way it would be defined for an
- integer argument (except that such a function would be of no use as it would overflow for
- any argument different of 0 and 1).
func f(x: ModInt): ModInt = x^100 + x + 1
- ———————————————————————————————————————————————————————————————————————————————————————————————————
when isMainModule:
var x = initModInt[13](10) echo &"f({x}) = {x}^100 + {x} + 1 = {f(x)}."</lang>
- Output:
f(10₁₃) = 10₁₃^100 + 10₁₃ + 1 = 1₁₃.
PARI/GP
This feature exists natively in GP: <lang parigp>Mod(3,7)+Mod(4,7)</lang>
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)
Phix
Phix does not allow operator overloading, but an f() which is agnostic about whether its parameter is a modular or normal int, we can do.
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})
- Output:
x^100 + x + 1 for x == 10 is 1e+100 x^100 + x + 1 for x == modint(10,13) is modint(1,13)
Prolog
Works with SWI-Prolog versin 6.4.1 and module lambda (found there : http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl ). <lang Prolog>:- use_module(library(lambda)).
congruence(Congruence, In, Fun, Out) :- maplist(Congruence +\X^Y^(Y is X mod Congruence), In, In1), call(Fun, In1, Out1), maplist(Congruence +\X^Y^(Y is X mod Congruence), Out1, Out).
fun_1([X], [Y]) :- Y is X^100 + X + 1.
fun_2(L, [R]) :- sum_list(L, R). </lang>
- Output:
?- congruence(13, [10], fun_1, R). R = [1]. ?- congruence(13, [10, 15, 13, 9, 22], fun_2, R). R = [4]. ?- congruence(13, [10, 15, 13, 9, 22], maplist(\X^Y^(Y is X * 13)), R). R = [0,0,0,0,0].
Python
We need to implement a Modulo type first, then give one of its instances to the "f" function.
Thanks to duck typing, the function doesn't need to care about the actual type it's given. We also use the dynamic nature of Python to dynamically build the operator overload methods and avoid repeating very similar code.
<lang Python>import operator import functools
@functools.total_ordering class Mod:
__slots__ = ['val','mod']
def __init__(self, val, mod): if not isinstance(val, int): raise ValueError('Value must be integer') if not isinstance(mod, int) or mod<=0: raise ValueError('Modulo must be positive integer') self.val = val % mod self.mod = mod
def __repr__(self): return 'Mod({}, {})'.format(self.val, self.mod)
def __int__(self): return self.val
def __eq__(self, other): if isinstance(other, Mod): if self.mod == other.mod: return self.val==other.val else: return NotImplemented elif isinstance(other, int): return self.val == other else: return NotImplemented
def __lt__(self, other): if isinstance(other, Mod): if self.mod == other.mod: return self.val<other.val else: return NotImplemented elif isinstance(other, int): return self.val < other else: return NotImplemented
def _check_operand(self, other): if not isinstance(other, (int, Mod)): raise TypeError('Only integer and Mod operands are supported') if isinstance(other, Mod) and self.mod != other.mod: raise ValueError('Inconsistent modulus: {} vs. {}'.format(self.mod, other.mod))
def __pow__(self, other): self._check_operand(other) # We use the built-in modular exponentiation function, this way we can avoid working with huge numbers. return Mod(pow(self.val, int(other), self.mod), self.mod)
def __neg__(self): return Mod(self.mod - self.val, self.mod)
def __pos__(self): return self # The unary plus operator does nothing.
def __abs__(self): return self # The value is always kept non-negative, so the abs function should do nothing.
- Helper functions to build common operands based on a template.
- They need to be implemented as functions for the closures to work properly.
def _make_op(opname):
op_fun = getattr(operator, opname) # Fetch the operator by name from the operator module def op(self, other): self._check_operand(other) return Mod(op_fun(self.val, int(other)) % self.mod, self.mod) return op
def _make_reflected_op(opname):
op_fun = getattr(operator, opname) def op(self, other): self._check_operand(other) return Mod(op_fun(int(other), self.val) % self.mod, self.mod) return op
- Build the actual operator overload methods based on the template.
for opname, reflected_opname in [('__add__', '__radd__'), ('__sub__', '__rsub__'), ('__mul__', '__rmul__')]:
setattr(Mod, opname, _make_op(opname)) setattr(Mod, reflected_opname, _make_reflected_op(opname))
def f(x):
return x**100+x+1
print(f(Mod(10,13)))
- Output: Mod(1, 13)</lang>
Quackery
Quackery is an extensible assembler for the Quackery Virtual Processor, which is implemented in Python3 (but could be implemented in any language). The QVP recognises three static types; Number (Python Int), Nest (Python List) and Operator (Python function). Adding more static types would require adding functionality to the QVP by modifying the source code for Quackery.
However it is possible to extend the assembler to include dynamic typing without modifying the QVP. The first part of the code presented here adds just sufficient dynamic typing to Quackery to fulfil the requirements of this task. It could be considered a first sketch towards adding more comprehensive dynamic typing to Quackery.
The second part of the code uses this to overload the Quackery words + and **.
The third part fulfils the requirements of this task.
<lang Quackery >[ stack ] is modulus ( --> s )
[ this ] is modular ( --> [ )
[ modulus share mod
modular nested join ] is modularise ( n --> N )
[ dup nest? iff
[ -1 peek modular oats ] else [ drop false ] ] is modular? ( N --> b )
[ modular? swap
modular? or ] is 2modular? ( N N --> b )
[ dup modular? if [ 0 peek ] ] is demodularise ( N --> n )
[ demodularise swap
demodularise swap ] is 2demodularise ( N N --> n )
[ dup $ = if
[ $ '"modularify(2-->1)" ' $ "needs a name after it." join message put bail ] nextword $ "[ 2dup 2modular? iff [ 2demodularise " over join $ " modularise ] else " join over join $ " ] is " join swap join space join swap join ] builds modularify(2-->1) ( --> )
( --------------------------------------------------------------- )
modularify(2-->1) + ( N N --> N )
modularify(2-->1) ** ( N N --> N )
( --------------------------------------------------------------- )
[ dup 100 ** + 1 + ] is f ( N --> N )
13 modulus put 10 f echo cr 10 modularise f echo modulus release cr</lang> Output:
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011 [ 1 modular ]
Racket
<lang racket>#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>
Raku
(formerly Perl 6) There is an ecosystem module called Modular which works basically as Perl 5's Math::ModInt. <lang perl6>use Modular; sub f(\x) { x**100 + x + 1}; say f( 10 Mod 13 )</lang>
- Output:
1 「mod 13」
Red
This implementation of +,-,*,/ uses a loose test (object?) to check operands type. As soon as one is a modular integer, the other one is treated as a modular integer too. <lang Red>Red ["Modular arithmetic"]
- defining the modular integer class, and a constructor
modulus: 13 m: function [n] [ either object? n [make n []] [context [val: n % modulus]] ]
- redefining operators +, -, *, / to include modular integers
foreach [op fun][+ add - subtract * multiply / divide][ set op make op! function [a b] compose/deep [ either any [object? a object? b][ a: m a b: m b m (fun) a/val b/val ][(fun) a b] ] ]
- redefining power - ** ; second operand must be an integer
- make op! function [a n] [
either object? a [ tmp: 1 loop n [tmp: tmp * a/val % modulus] m tmp ][power a n] ]
- testing
f: function [x] [x ** 100 + x + 1] print ["f definition is:" mold :f] print ["f((integer) 10) is:" f 10] print ["f((modular) 10) is: (modular)" f m 10]</lang>
- Output:
f definition is: func [x][x ** 100 + x + 1] f((integer) 10) is: 1.0e100 f((modular) 10) is: (modular) val: 1
Ruby
<lang ruby># stripped version of Andrea Fazzi's submission to Ruby Quiz #179
class Modulo
include Comparable
def initialize(n = 0, m = 13) @n, @m = n % m, m end
def to_i @n end def <=>(other_n) @n <=> other_n.to_i end
[:+, :-, :*, :**].each do |meth| define_method(meth) { |other_n| Modulo.new(@n.send(meth, other_n.to_i), @m) } end
def coerce(numeric) [numeric, @n] end
end
- Demo
x, y = Modulo.new(10), Modulo.new(20)
p x > y # true p x == y # false p [x,y].sort #[#<Modulo:0x000000012ae0f8 @n=7, @m=13>, #<Modulo:0x000000012ae148 @n=10, @m=13>] p x + y ##<Modulo:0x0000000117e110 @n=4, @m=13> p 2 + y # 9 p y + 2 ##<Modulo:0x00000000ad1d30 @n=9, @m=13>
p x**100 + x +1 ##<Modulo:0x00000000ad1998 @n=1, @m=13> </lang>
Scala
- Output:
Best seen running in your browser either by ScalaFiddle (ES aka JavaScript, non JVM) or Scastie (remote JVM).
<lang Scala>object ModularArithmetic extends App {
private val x = new ModInt(10, 13) private val y = f(x)
private def f[T](x: Ring[T]) = (x ^ 100) + x + x.one
private trait Ring[T] { def +(rhs: Ring[T]): Ring[T]
def *(rhs: Ring[T]): Ring[T]
def one: Ring[T]
def ^(p: Int): Ring[T] = { require(p >= 0, "p must be zero or greater") var pp = p var pwr = this.one while ( { pp -= 1; pp } >= 0) pwr = pwr * this pwr } }
private class ModInt(var value: Int, var modulo: Int) extends Ring[ModInt] { def +(other: Ring[ModInt]): Ring[ModInt] = { require(other.isInstanceOf[ModInt], "Cannot add an unknown ring.") val rhs = other.asInstanceOf[ModInt] require(modulo == rhs.modulo, "Cannot add rings with different modulus") new ModInt((value + rhs.value) % modulo, modulo) }
def *(other: Ring[ModInt]): Ring[ModInt] = { require(other.isInstanceOf[ModInt], "Cannot multiple an unknown ring.") val rhs = other.asInstanceOf[ModInt] require(modulo == rhs.modulo, "Cannot multiply rings with different modulus") new ModInt((value * rhs.value) % modulo, modulo) }
override def one = new ModInt(1, modulo)
override def toString: String = f"ModInt($value%d, $modulo%d)" }
println("x ^ 100 + x + 1 for x = ModInt(10, 13) is " + y)
}</lang>
Sidef
<lang ruby>class Modulo(n=0, m=13) {
method init { (n, m) = (n % m, m) }
method to_n { n }
< + - * ** >.each { |meth| Modulo.def_method(meth, method(n2) { Modulo(n.(meth)(n2.to_n), m) }) }
method to_s { "#{n} 「mod #{m}」" }
}
func f(x) { x**100 + x + 1 } say f(Modulo(10, 13))</lang>
- Output:
1 「mod 13」
Swift
<lang swift>precedencegroup ExponentiationGroup {
higherThan: MultiplicationPrecedence
}
infix operator ** : ExponentiationGroup
protocol Ring {
associatedtype RingType: Numeric
var one: Self { get }
static func +(_ lhs: Self, _ rhs: Self) -> Self static func *(_ lhs: Self, _ rhs: Self) -> Self static func **(_ lhs: Self, _ rhs: Int) -> Self
}
extension Ring {
static func **(_ lhs: Self, _ rhs: Int) -> Self { var ret = lhs.one
for _ in stride(from: rhs, to: 0, by: -1) { ret = ret * lhs }
return ret }
}
struct ModInt: Ring {
typealias RingType = Int
var value: Int var modulo: Int
var one: ModInt { ModInt(1, modulo: modulo) }
init(_ value: Int, modulo: Int) { self.value = value self.modulo = modulo }
static func +(lhs: ModInt, rhs: ModInt) -> ModInt { precondition(lhs.modulo == rhs.modulo)
return ModInt((lhs.value + rhs.value) % lhs.modulo, modulo: lhs.modulo) }
static func *(lhs: ModInt, rhs: ModInt) -> ModInt { precondition(lhs.modulo == rhs.modulo)
return ModInt((lhs.value * rhs.value) % lhs.modulo, modulo: lhs.modulo) }
}
func f<T: Ring>(_ x: T) -> T { (x ** 100) + x + x.one }
let x = ModInt(10, modulo: 13) let y = f(x)
print("x ^ 100 + x + 1 for x = ModInt(10, 13) is \(y)")</lang>
- Output:
x ^ 100 + x + 1 for x = ModInt(10, 13) is ModInt(value: 1, modulo: 13)
Tcl
Tcl does not permit overriding of operators, but does not force an expression
to be evaluated as a standard expression.
Creating a parser and custom evaluation engine is relatively straight-forward,
as is shown here.
<lang tcl>package require Tcl 8.6 package require pt::pgen
- A simple expression parser for a subset of Tcl's expression language
- Define the grammar of expressions that we want to handle
set grammar { PEG Calculator (Expression)
Expression <- Term (' '* AddOp ' '* Term)* ; Term <- Factor (' '* MulOp ' '* Factor)* ; Fragment <- '(' ' '* Expression ' '* ')' / Number / Var ; Factor <- Fragment (' '* PowOp ' '* Fragment)* ; Number <- Sign? Digit+ ; Var <- '$' ( 'x'/'y'/'z' ) ;
Digit <- '0'/'1'/'2'/'3'/'4'/'5'/'6'/'7'/'8'/'9' ; Sign <- '-' / '+' ; MulOp <- '*' / '/' ; AddOp <- '+' / '-' ; PowOp <- '**' ;
END; }
- Instantiate the parser class
catch [pt::pgen peg $grammar snit -class Calculator -name Grammar]
- An engine that compiles an expression into Tcl code
oo::class create CompileAST {
variable sourcecode opns constructor {semantics} {
set opns $semantics
} method compile {script} {
# Instantiate the parser set c [Calculator] set sourcecode $script try { return [my {*}[$c parset $script]] } finally { $c destroy }
}
method Expression-Empty args {} method Expression-Compound {from to args} {
foreach {o p} [list Expression-Empty {*}$args] { set o [my {*}$o]; set p [my {*}$p] set v [expr {$o ne "" ? "$o \[$v\] \[$p\]" : $p}] } return $v
} forward Expression my Expression-Compound forward Term my Expression-Compound forward Factor my Expression-Compound forward Fragment my Expression-Compound
method Expression-Operator {from to args} {
list ${opns} [string range $sourcecode $from $to]
} forward AddOp my Expression-Operator forward MulOp my Expression-Operator forward PowOp my Expression-Operator
method Number {from to args} {
list ${opns} value [string range $sourcecode $from $to]
}
method Var {from to args} {
list ${opns} variable [string range $sourcecode [expr {$from+1}] $to]
}
}</lang> None of the code above knows about modular arithmetic at all, or indeed about actual expression evaluation. Now we define the semantics that we want to actually use. <lang tcl># The semantic evaluation engine; this is the part that knows mod arithmetic oo::class create ModEval {
variable mod constructor {modulo} {set mod $modulo} method value {literal} {return [expr {$literal}]} method variable {name} {return [expr {[set ::$name]}]} method + {a b} {return [expr {($a + $b) % $mod}]} method - {a b} {return [expr {($a - $b) % $mod}]} method * {a b} {return [expr {($a * $b) % $mod}]} method / {a b} {return [expr {($a / $b) % $mod}]} method ** {a b} {
# Tcl supports bignums natively, so we use the naive version return [expr {($a ** $b) % $mod}]
} export + - * / **
}
- Put all the pieces together
set comp [CompileAST new [ModEval create mod13 13]]</lang> Finally, demonstrating… <lang tcl>set compiled [$comp compile {$x**100 + $x + 1}] set x 10 puts "[eval $compiled] = $compiled"</lang>
- Output:
1 = ::mod13 + [::mod13 + [::mod13 ** [::mod13 variable x] [::mod13 value 100]] [::mod13 variable x]] [::mod13 value 1]
VBA
<lang vb>Option Base 1
Private Function mi_one(ByVal a As Variant) As Variant
If IsArray(a) Then a(1) = 1 Else a = 1 End If mi_one = a
End Function
Private Function mi_add(ByVal a As Variant, b As Variant) As Variant
If IsArray(a) Then If IsArray(b) Then If a(2) <> b(2) Then mi_add = CVErr(2019) Else a(1) = (a(1) + b(1)) Mod a(2) mi_add = a End If Else mi_add = CVErr(2018) End If Else If IsArray(b) Then mi_add = CVErr(2018) Else a = a + b mi_add = a End If End If
End Function
Private Function mi_mul(ByVal a As Variant, b As Variant) As Variant
If IsArray(a) Then If IsArray(b) Then If a(2) <> b(2) Then mi_mul = CVErr(2019) Else a(1) = (a(1) * b(1)) Mod a(2) mi_mul = a End If Else mi_mul = CVErr(2018) End If Else If IsArray(b) Then mi_mul = CVErr(2018) Else a = a * b mi_mul = a End If End If
End Function
Private Function mi_power(x As Variant, p As Integer) As Variant
res = mi_one(x) For i = 1 To p res = mi_mul(res, x) Next i mi_power = res
End Function
Private Function mi_print(m As Variant) As Variant
If IsArray(m) Then s = "modint(" & m(1) & "," & m(2) & ")" Else s = CStr(m) End If mi_print = s
End Function
Private Function f(x As Variant) As Variant
f = mi_add(mi_power(x, 100), mi_add(x, mi_one(x)))
End Function
Private Sub test(x As Variant)
Debug.Print "x^100 + x + 1 for x == " & mi_print(x) & " is " & mi_print(f(x))
End Sub Public Sub main()
test 10 test [{10,13}]
End Sub</lang>
- Output:
x^100 + x + 1 for x == 10 is 1E+100 x^100 + x + 1 for x == modint(10,13) is modint(1,13)
Visual Basic .NET
<lang vbnet>Module Module1
Interface IAddition(Of T) Function Add(rhs As T) As T End Interface
Interface IMultiplication(Of T) Function Multiply(rhs As T) As T End Interface
Interface IPower(Of T) Function Power(pow As Integer) As T End Interface
Interface IOne(Of T) Function One() As T End Interface
Class ModInt Implements IAddition(Of ModInt), IMultiplication(Of ModInt), IPower(Of ModInt), IOne(Of ModInt)
Sub New(value As Integer, modulo As Integer) Me.Value = value Me.Modulo = modulo End Sub
ReadOnly Property Value As Integer ReadOnly Property Modulo As Integer
Public Function Add(rhs As ModInt) As ModInt Implements IAddition(Of ModInt).Add Return Me + rhs End Function
Public Function Multiply(rhs As ModInt) As ModInt Implements IMultiplication(Of ModInt).Multiply Return Me * rhs End Function
Public Function Power(pow_ As Integer) As ModInt Implements IPower(Of ModInt).Power Return Pow(Me, pow_) End Function
Public Function One() As ModInt Implements IOne(Of ModInt).One Return New ModInt(1, Modulo) End Function
Public Overrides Function ToString() As String Return String.Format("ModInt({0}, {1})", Value, Modulo) End Function
Public Shared Operator +(lhs As ModInt, rhs As ModInt) As ModInt If lhs.Modulo <> rhs.Modulo Then Throw New ArgumentException("Cannot add rings with different modulus") End If Return New ModInt((lhs.Value + rhs.Value) Mod lhs.Modulo, lhs.Modulo) End Operator
Public Shared Operator *(lhs As ModInt, rhs As ModInt) As ModInt If lhs.Modulo <> rhs.Modulo Then Throw New ArgumentException("Cannot multiply rings with different modulus") End If Return New ModInt((lhs.Value * rhs.Value) Mod lhs.Modulo, lhs.Modulo) End Operator
Public Shared Function Pow(self As ModInt, p As Integer) As ModInt If p < 0 Then Throw New ArgumentException("p must be zero or greater") End If
Dim pp = p Dim pwr = self.One() While pp > 0 pp -= 1 pwr *= self End While Return pwr End Function End Class
Function F(Of T As {IAddition(Of T), IMultiplication(Of T), IPower(Of T), IOne(Of T)})(x As T) As T Return x.Power(100).Add(x).Add(x.One) End Function
Sub Main() Dim x As New ModInt(10, 13) Dim y = F(x) Console.WriteLine("x ^ 100 + x + 1 for x = {0} is {1}", x, y) End Sub
End Module</lang>
- Output:
x ^ 100 + x + 1 for x = ModInt(10, 13) is ModInt(1, 13)
Wren
<lang ecmascript>// Semi-abstract though we can define a 'pow' method in terms of the other operations. class Ring {
+(other) {} *(other) {} one {}
pow(p) { if (p.type != Num || !p.isInteger || p < 0) { Fiber.abort("Argument must be non-negative integer.") } var pwr = one while (p > 0) { pwr = pwr * this p = p - 1 } return pwr }
}
class ModInt is Ring {
construct new(value, modulo) { _value = value _modulo = modulo }
value { _value } modulo { _modulo }
+(other) { if (other.type != ModInt || _modulo != other.modulo) { Fiber.abort("Argument must be a ModInt with the same modulus.") } return ModInt.new((_value + other.value) % _modulo, _modulo) }
*(other) { if (other.type != ModInt || _modulo != other.modulo) { Fiber.abort("Argument must be a ModInt with the same modulus.") } return ModInt.new((_value * other.value) % _modulo, _modulo) }
one { ModInt.new(1, _modulo) }
toString { "Modint(%(_value), %(_modulo))" }
}
var f = Fn.new { |x|
if (!(x is Ring)) Fiber.abort("Argument must be a Ring.") return x.pow(100) + x + x.one
}
var x = ModInt.new(10, 13) System.print("x^100 + x + 1 for x = %(x) is %(f.call(x))")</lang>
- Output:
x^100 + x + 1 for x = Modint(10, 13) is Modint(1, 13)
zkl
Doing just enough to perform the task: <lang zkl>class MC{
fcn init(n,mod){ var N=n,M=mod; } fcn toString { String(N.divr(M)[1],"M",M) } fcn pow(p) { self( N.pow(p).divr(M)[1], M ) } fcn __opAdd(mc){ if(mc.isType(Int)) z:=N+mc; else z:=N*M + mc.N*mc.M; self(z.divr(M)[1],M) }
}</lang> Using GNU GMP lib to do the big math (to avoid writing a powmod function): <lang zkl>var BN=Import("zklBigNum"); fcn f(n){ n.pow(100) + n + 1 } f(1).println(" <-- 1^100 + 1 + 1"); n:=MC(BN(10),13); (n+3).println(" <-- 10M13 + 3"); f(n).println(" <-- 10M13^100 + 10M13 + 1");</lang>
- Output:
3 <-- 1^100 + 1 + 1 0M13 <-- 10M13 + 3 1M13 <-- 10M13^100 + 10M13 + 1