Modular arithmetic: Difference between revisions
Content added Content deleted
(Added Wren) |
|||
Line 1,109: | Line 1,109: | ||
{{out}} |
{{out}} |
||
<pre>1 「mod 13」</pre> |
<pre>1 「mod 13」</pre> |
||
=={{header|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:" f m 10]</lang> |
|||
{{out}} |
|||
<pre>f definition is: func [x][x ** 100 + x + 1] |
|||
f(integer 10) is: 1.0e100 |
|||
f(modular 10) is: val: 1 |
|||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |