Modular arithmetic: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) (→{{header|Raku}}: in-line code from module that cannot be installed (more instructive to boot)) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 24: | Line 24: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Ada has modular types. |
Ada has modular types. |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Modular_Demo is |
procedure Modular_Demo is |
||
Line 47: | Line 47: | ||
Put (F_10); Put (" mod "); Put (Modul_13'Modulus'Image); |
Put (F_10); Put (" mod "); Put (Modul_13'Modulus'Image); |
||
New_Line; |
New_Line; |
||
end Modular_Demo;</ |
end Modular_Demo;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 54: | Line 54: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}} |
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}} |
||
< |
<syntaxhighlight lang="algol68"># allow for large integers in Algol 68G # |
||
PR precision 200 PR |
PR precision 200 PR |
||
Line 75: | Line 75: | ||
ESAC; |
ESAC; |
||
print( ( whole( f( MODULARINT( 10, 13 ) ), 0 ), newline ) )</ |
print( ( whole( f( MODULARINT( 10, 13 ) ), 0 ), newline ) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 83: | Line 83: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
struct ModularArithmetic { |
struct ModularArithmetic { |
||
Line 134: | Line 134: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>f(ModularInteger(10, 13)) = ModularInteger(1, 13)</pre> |
<pre>f(ModularInteger(10, 13)) = ModularInteger(1, 13)</pre> |
||
Line 140: | Line 140: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
namespace ModularArithmetic { |
namespace ModularArithmetic { |
||
Line 225: | Line 225: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>x ^ 100 + x + 1 for x = ModInt(10, 13) is ModInt(1, 13)</pre> |
<pre>x ^ 100 + x + 1 for x = ModInt(10, 13) is ModInt(1, 13)</pre> |
||
Line 231: | Line 231: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <ostream> |
#include <ostream> |
||
Line 305: | Line 305: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>f(ModularInteger(10, 13)) = ModularInteger(1, 13)</pre> |
<pre>f(ModularInteger(10, 13)) = ModularInteger(1, 13)</pre> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio; |
||
version(unittest) { |
version(unittest) { |
||
Line 436: | Line 436: | ||
assertEquals(b^^99, ModularInteger(12,13)); |
assertEquals(b^^99, ModularInteger(12,13)); |
||
assertEquals(b^^100, ModularInteger(3,13)); |
assertEquals(b^^100, ModularInteger(3,13)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 457: | Line 457: | ||
Also note that since <code>^</code> is not a generic word, we employ the strategy of renaming it to <code>**</code> inside our vocabulary and defining a new word named <code>^</code> 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. |
Also note that since <code>^</code> is not a generic word, we employ the strategy of renaming it to <code>**</code> inside our vocabulary and defining a new word named <code>^</code> 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. |
||
< |
<syntaxhighlight lang="factor">USING: accessors generalizations io kernel math math.functions |
||
parser prettyprint prettyprint.custom sequences ; |
parser prettyprint prettyprint.custom sequences ; |
||
IN: rosetta-code.modular-arithmetic |
IN: rosetta-code.modular-arithmetic |
||
Line 544: | Line 544: | ||
} 2show ; |
} 2show ; |
||
MAIN: modular-arithmetic-demo</ |
MAIN: modular-arithmetic-demo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 616: | Line 616: | ||
=={{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. |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 678: | Line 678: | ||
func main() { |
func main() { |
||
fmt.Println(f(modRing(13), uint(10))) |
fmt.Println(f(modRing(13), uint(10))) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 685: | Line 685: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight 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 |
-- 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.) |
-- also provides the fancy Zahlen (ℤ) symbol as a synonym for Integer.) |
||
Line 698: | Line 698: | ||
main :: IO () |
main :: IO () |
||
main = print (f 10)</ |
main = print (f 10)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 710: | Line 710: | ||
J does not allow "operator redefinition", but J's operators are capable of operating consistently on values which represent modular integers: |
J does not allow "operator redefinition", but J's operators are capable of operating consistently on values which represent modular integers: |
||
< |
<syntaxhighlight lang="j"> f=: (+./1 1,:_101{.1x)&p.</syntaxhighlight> |
||
Task example: |
Task example: |
||
< |
<syntaxhighlight lang="j"> 13|f 10 |
||
1</ |
1</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{works with|Java|8}} |
{{works with|Java|8}} |
||
< |
<syntaxhighlight lang="java">public class ModularArithmetic { |
||
private interface Ring<T> { |
private interface Ring<T> { |
||
Ring<T> plus(Ring<T> rhs); |
Ring<T> plus(Ring<T> rhs); |
||
Line 804: | Line 804: | ||
System.out.flush(); |
System.out.flush(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>x ^ 100 + x + 1 for x = ModInt(10, 13) is ModInt(1, 13)</pre> |
<pre>x ^ 100 + x + 1 for x = ModInt(10, 13) is ModInt(1, 13)</pre> |
||
Line 829: | Line 829: | ||
'''Preliminaries''' |
'''Preliminaries''' |
||
< |
<syntaxhighlight lang="jq">def assert($e; $msg): if $e then . else "assertion violation @ \($msg)" | error end; |
||
def is_integer: type=="number" and floor == .; |
def is_integer: type=="number" and floor == .; |
||
Line 835: | Line 835: | ||
# To take advantage of gojq's arbitrary-precision integer arithmetic: |
# To take advantage of gojq's arbitrary-precision integer arithmetic: |
||
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); |
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Modular Arithmetic''' |
'''Modular Arithmetic''' |
||
< |
<syntaxhighlight 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. |
# The function modint::assert/0 checks the input is of this form with integer values. |
||
Line 874: | Line 874: | ||
# pretty print |
# pretty print |
||
def modint::pp: "«\(.value) % \(.mod)»";</ |
def modint::pp: "«\(.value) % \(.mod)»";</syntaxhighlight> |
||
''' Ring Functions''' |
''' Ring Functions''' |
||
< |
<syntaxhighlight lang="jq">def ring::add($A; $B): |
||
if $A|is_modint then modint::add($A; $B) |
if $A|is_modint then modint::add($A; $B) |
||
elif $A|is_integer then $A + $B |
elif $A|is_integer then $A + $B |
||
Line 901: | Line 901: | ||
def ring::f($x): |
def ring::f($x): |
||
ring::add( ring::add( ring::pow($x; 100); $x); 1);</ |
ring::add( ring::add( ring::pow($x; 100); $x); 1);</syntaxhighlight> |
||
'''Evaluating ring::f''' |
'''Evaluating ring::f''' |
||
< |
<syntaxhighlight lang="jq">def main: |
||
(ring::f(1) | "f(\(1)) => \(.)"), |
(ring::f(1) | "f(\(1)) => \(.)"), |
||
(modint::make(10;13) |
(modint::make(10;13) |
||
| ring::f(.) as $out |
| ring::f(.) as $out |
||
| "f(\(ring::pp)) => \($out|ring::pp)");</ |
| "f(\(ring::pp)) => \($out|ring::pp)");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 919: | Line 919: | ||
Implements the <tt>Modulo</tt> struct and basic operations. |
Implements the <tt>Modulo</tt> struct and basic operations. |
||
< |
<syntaxhighlight lang="julia">struct Modulo{T<:Integer} <: Integer |
||
val::T |
val::T |
||
mod::T |
mod::T |
||
Line 946: | Line 946: | ||
f(x) = x ^ 100 + x + 1 |
f(x) = x ^ 100 + x + 1 |
||
@show f(modulo(10, 13))</ |
@show f(modulo(10, 13))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 952: | Line 952: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.3 |
||
interface Ring<T> { |
interface Ring<T> { |
||
Line 992: | Line 992: | ||
val y = f(x) |
val y = f(x) |
||
println("x ^ 100 + x + 1 for x == ModInt(10, 13) is $y") |
println("x ^ 100 + x + 1 for x == ModInt(10, 13) is $y") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,000: | Line 1,000: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function make(value, modulo) |
||
local v = value % modulo |
local v = value % modulo |
||
local tbl = {value=v, modulo=modulo} |
local tbl = {value=v, modulo=modulo} |
||
Line 1,060: | Line 1,060: | ||
local x = make(10, 13) |
local x = make(10, 13) |
||
local y = func(x) |
local y = func(x) |
||
print("x ^ 100 + x + 1 for "..x.." is "..y)</ |
print("x ^ 100 + x + 1 for "..x.." is "..y)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>x ^ 100 + x + 1 for ModInt(10, 13) is ModInt(1, 13)</pre> |
<pre>x ^ 100 + x + 1 for ModInt(10, 13) is ModInt(1, 13)</pre> |
||
Line 1,066: | Line 1,066: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
The best way to do it is probably to use the finite fields package. |
The best way to do it is probably to use the finite fields package. |
||
< |
<syntaxhighlight lang="mathematica"><< FiniteFields` |
||
x^100 + x + 1 /. x -> GF[13]@{10}</ |
x^100 + x + 1 /. x -> GF[13]@{10}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
{1}<sub>13</sub> |
{1}<sub>13</sub> |
||
Line 1,073: | Line 1,073: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
Modular integers are represented as distinct integers with a modulus N managed by the compiler. |
Modular integers are represented as distinct integers with a modulus N managed by the compiler. |
||
< |
<syntaxhighlight lang="nim">import macros, sequtils, strformat, strutils |
||
const Subscripts: array['0'..'9', string] = ["₀", "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉"] |
const Subscripts: array['0'..'9', string] = ["₀", "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉"] |
||
Line 1,147: | Line 1,147: | ||
var x = initModInt[13](10) |
var x = initModInt[13](10) |
||
echo &"f({x}) = {x}^100 + {x} + 1 = {f(x)}."</ |
echo &"f({x}) = {x}^100 + {x} + 1 = {f(x)}."</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,155: | Line 1,155: | ||
This feature exists natively in GP: |
This feature exists natively in GP: |
||
< |
<syntaxhighlight lang="parigp">Mod(3,7)+Mod(4,7)</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
There is a CPAN module called Math::ModInt which does the job. |
There is a CPAN module called Math::ModInt which does the job. |
||
< |
<syntaxhighlight lang="perl">use Math::ModInt qw(mod); |
||
sub f { my $x = shift; $x**100 + $x + 1 }; |
sub f { my $x = shift; $x**100 + $x + 1 }; |
||
print f mod(10, 13);</ |
print f mod(10, 13);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>mod(1, 13)</pre> |
<pre>mod(1, 13)</pre> |
||
Line 1,168: | Line 1,168: | ||
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. |
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. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">type</span> <span style="color: #000000;">mi</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">type</span> <span style="color: #000000;">mi</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">return</span> <span style="color: #004080;">sequence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">2</span> <span style="color: #008080;">and</span> <span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">and</span> <span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span> |
<span style="color: #008080;">return</span> <span style="color: #004080;">sequence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">2</span> <span style="color: #008080;">and</span> <span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">and</span> <span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span> |
||
Line 1,223: | Line 1,223: | ||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> |
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #000000;">test</span><span style="color: #0000FF;">({</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">})</span> |
<span style="color: #000000;">test</span><span style="color: #0000FF;">({</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
Line 1,233: | Line 1,233: | ||
=={{header|Prolog}}== |
=={{header|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 ). |
Works with SWI-Prolog versin 6.4.1 and module lambda (found there : http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl ). |
||
< |
<syntaxhighlight lang="prolog">:- use_module(library(lambda)). |
||
congruence(Congruence, In, Fun, Out) :- |
congruence(Congruence, In, Fun, Out) :- |
||
Line 1,245: | Line 1,245: | ||
fun_2(L, [R]) :- |
fun_2(L, [R]) :- |
||
sum_list(L, R). |
sum_list(L, R). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> ?- congruence(13, [10], fun_1, R). |
<pre> ?- congruence(13, [10], fun_1, R). |
||
Line 1,264: | Line 1,264: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="python">import operator |
||
import functools |
import functools |
||
Line 1,352: | Line 1,352: | ||
print(f(Mod(10,13))) |
print(f(Mod(10,13))) |
||
# Output: Mod(1, 13)</ |
# Output: Mod(1, 13)</syntaxhighlight> |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
Line 1,364: | Line 1,364: | ||
The third part fulfils the requirements of this task. |
The third part fulfils the requirements of this task. |
||
< |
<syntaxhighlight lang="quackery ">[ stack ] is modulus ( --> s ) |
||
[ this ] is modular ( --> [ ) |
[ this ] is modular ( --> [ ) |
||
Line 1,409: | Line 1,409: | ||
10 f echo cr |
10 f echo cr |
||
10 modularise f echo |
10 modularise f echo |
||
modulus release cr</ |
modulus release cr</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
<pre>10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011 |
<pre>10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011 |
||
Line 1,418: | Line 1,418: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(require racket/require |
(require racket/require |
||
;; grab all "mod*" names, but get them without the "mod", so |
;; grab all "mod*" names, but get them without the "mod", so |
||
Line 1,428: | Line 1,428: | ||
(define (f x) (+ (expt x 100) x 1)) |
(define (f x) (+ (expt x 100) x 1)) |
||
(with-modulus 13 (f 10)) |
(with-modulus 13 (f 10)) |
||
;; => 1</ |
;; => 1</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 1,434: | Line 1,434: | ||
Relevant portions of code in-lined from [https://raku.land/github:grondilu/Modular Modular]. |
Relevant portions of code in-lined from [https://raku.land/github:grondilu/Modular Modular]. |
||
<lang |
<syntaxhighlight lang="raku" line>class Modulo does Real { |
||
has ($.residue, $.modulus); |
has ($.residue, $.modulus); |
||
multi method new($n, :$modulus) { self.new: :residue($n % $modulus), :$modulus } |
multi method new($n, :$modulus) { self.new: :residue($n % $modulus), :$modulus } |
||
Line 1,458: | Line 1,458: | ||
sub f(\x) { x**100 + x + 1}; |
sub f(\x) { x**100 + x + 1}; |
||
my $m-new = f( 10 Mod 13 ); |
my $m-new = f( 10 Mod 13 ); |
||
say f( 10 Mod 13 );</ |
say f( 10 Mod 13 );</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 「mod 13」</pre> |
<pre>1 「mod 13」</pre> |
||
Line 1,465: | Line 1,465: | ||
This implementation of +,-,*,/ uses a loose test (object?) to check operands type. |
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. |
As soon as one is a modular integer, the other one is treated as a modular integer too. |
||
< |
<syntaxhighlight lang="red">Red ["Modular arithmetic"] |
||
; defining the modular integer class, and a constructor |
; defining the modular integer class, and a constructor |
||
Line 1,494: | Line 1,494: | ||
print ["f definition is:" mold :f] |
print ["f definition is:" mold :f] |
||
print ["f((integer) 10) is:" f 10] |
print ["f((integer) 10) is:" f 10] |
||
print ["f((modular) 10) is: (modular)" f m 10]</ |
print ["f((modular) 10) is: (modular)" f m 10]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>f definition is: func [x][x ** 100 + x + 1] |
<pre>f definition is: func [x][x ** 100 + x + 1] |
||
Line 1,501: | Line 1,501: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby"># stripped version of Andrea Fazzi's submission to Ruby Quiz #179 |
||
class Modulo |
class Modulo |
||
Line 1,539: | Line 1,539: | ||
p x**100 + x +1 ##<Modulo:0x00000000ad1998 @n=1, @m=13> |
p x**100 + x +1 ##<Modulo:0x00000000ad1998 @n=1, @m=13> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/jOSxPQu/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/ZSeJw1lBRZiHenoQ6coDdA Scastie (remote JVM)]. |
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/jOSxPQu/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/ZSeJw1lBRZiHenoQ6coDdA Scastie (remote JVM)]. |
||
< |
<syntaxhighlight lang="scala">object ModularArithmetic extends App { |
||
private val x = new ModInt(10, 13) |
private val x = new ModInt(10, 13) |
||
private val y = f(x) |
private val y = f(x) |
||
Line 1,591: | Line 1,591: | ||
println("x ^ 100 + x + 1 for x = ModInt(10, 13) is " + y) |
println("x ^ 100 + x + 1 for x = ModInt(10, 13) is " + y) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">class Modulo(n=0, m=13) { |
||
method init { |
method init { |
||
Line 1,611: | Line 1,611: | ||
func f(x) { x**100 + x + 1 } |
func f(x) { x**100 + x + 1 } |
||
say f(Modulo(10, 13))</ |
say f(Modulo(10, 13))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 「mod 13」</pre> |
<pre>1 「mod 13」</pre> |
||
Line 1,619: | Line 1,619: | ||
{{trans|Scala}} |
{{trans|Scala}} |
||
< |
<syntaxhighlight lang="swift">precedencegroup ExponentiationGroup { |
||
higherThan: MultiplicationPrecedence |
higherThan: MultiplicationPrecedence |
||
} |
} |
||
Line 1,678: | Line 1,678: | ||
let y = f(x) |
let y = f(x) |
||
print("x ^ 100 + x + 1 for x = ModInt(10, 13) is \(y)")</ |
print("x ^ 100 + x + 1 for x = ModInt(10, 13) is \(y)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,690: | Line 1,690: | ||
as is shown here. |
as is shown here. |
||
{{tcllib|pt::pgen}} |
{{tcllib|pt::pgen}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.6 |
||
package require pt::pgen |
package require pt::pgen |
||
Line 1,762: | Line 1,762: | ||
list ${opns} variable [string range $sourcecode [expr {$from+1}] $to] |
list ${opns} variable [string range $sourcecode [expr {$from+1}] $to] |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
None of the code above knows about modular arithmetic at all, or indeed about actual expression evaluation. |
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. |
Now we define the semantics that we want to actually use. |
||
< |
<syntaxhighlight lang="tcl"># The semantic evaluation engine; this is the part that knows mod arithmetic |
||
oo::class create ModEval { |
oo::class create ModEval { |
||
variable mod |
variable mod |
||
Line 1,783: | Line 1,783: | ||
# Put all the pieces together |
# Put all the pieces together |
||
set comp [CompileAST new [ModEval create mod13 13]]</ |
set comp [CompileAST new [ModEval create mod13 13]]</syntaxhighlight> |
||
Finally, demonstrating… |
Finally, demonstrating… |
||
< |
<syntaxhighlight lang="tcl">set compiled [$comp compile {$x**100 + $x + 1}] |
||
set x 10 |
set x 10 |
||
puts "[eval $compiled] = $compiled"</ |
puts "[eval $compiled] = $compiled"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,794: | Line 1,794: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
{{trans|Phix}}< |
{{trans|Phix}}<syntaxhighlight lang="vb">Option Base 1 |
||
Private Function mi_one(ByVal a As Variant) As Variant |
Private Function mi_one(ByVal a As Variant) As Variant |
||
If IsArray(a) Then |
If IsArray(a) Then |
||
Line 1,875: | Line 1,875: | ||
test 10 |
test 10 |
||
test [{10,13}] |
test [{10,13}] |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre>x^100 + x + 1 for x == 10 is 1E+100 |
<pre>x^100 + x + 1 for x == 10 is 1E+100 |
||
x^100 + x + 1 for x == modint(10,13) is modint(1,13)</pre> |
x^100 + x + 1 for x == modint(10,13) is modint(1,13)</pre> |
||
Line 1,881: | Line 1,881: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="vbnet">Module Module1 |
||
Interface IAddition(Of T) |
Interface IAddition(Of T) |
||
Line 1,969: | Line 1,969: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>x ^ 100 + x + 1 for x = ModInt(10, 13) is ModInt(1, 13)</pre> |
<pre>x ^ 100 + x + 1 for x = ModInt(10, 13) is ModInt(1, 13)</pre> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang="ecmascript">// Semi-abstract though we can define a 'pow' method in terms of the other operations. |
||
class Ring { |
class Ring { |
||
+(other) {} |
+(other) {} |
||
Line 2,027: | Line 2,027: | ||
var x = ModInt.new(10, 13) |
var x = ModInt.new(10, 13) |
||
System.print("x^100 + x + 1 for x = %(x) is %(f.call(x))")</ |
System.print("x^100 + x + 1 for x = %(x) is %(f.call(x))")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,036: | Line 2,036: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Doing just enough to perform the task: |
Doing just enough to perform the task: |
||
< |
<syntaxhighlight lang="zkl">class MC{ |
||
fcn init(n,mod){ var N=n,M=mod; } |
fcn init(n,mod){ var N=n,M=mod; } |
||
fcn toString { String(N.divr(M)[1],"M",M) } |
fcn toString { String(N.divr(M)[1],"M",M) } |
||
Line 2,044: | Line 2,044: | ||
self(z.divr(M)[1],M) |
self(z.divr(M)[1],M) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Using GNU GMP lib to do the big math (to avoid writing a powmod function): |
Using GNU GMP lib to do the big math (to avoid writing a powmod function): |
||
< |
<syntaxhighlight lang="zkl">var BN=Import("zklBigNum"); |
||
fcn f(n){ n.pow(100) + n + 1 } |
fcn f(n){ n.pow(100) + n + 1 } |
||
f(1).println(" <-- 1^100 + 1 + 1"); |
f(1).println(" <-- 1^100 + 1 + 1"); |
||
n:=MC(BN(10),13); |
n:=MC(BN(10),13); |
||
(n+3).println(" <-- 10M13 + 3"); |
(n+3).println(" <-- 10M13 + 3"); |
||
f(n).println(" <-- 10M13^100 + 10M13 + 1");</ |
f(n).println(" <-- 10M13^100 + 10M13 + 1");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |