1
$\begingroup$

I want to implement something like 1 + 1 = 0; i.e., simple modular arithmetic in Mathematica.

This seems like it should be a really easy built-in option, but I can't find any info on it anywhere. I think probably I'm just overlooking something.

Really there should just be an option for Plus. But Options[Plus] gives { }.

$\endgroup$

2 Answers 2

2
$\begingroup$

Borrowing Rojo's idea from Is it possible to define custom compound assignment operators like ⊕= similar to built-ins +=, *= etc?, we can construct a modular addition operator.

MakeExpression[RowBox[{lhs_, rest : PatternSequence[SubscriptBox["⊕", m_], _] ..}],
   StandardForm] := 
 MakeExpression[{{lhs, rest}[[1 ;; ;; 2]], m}, 
   StandardForm] /. {x_, HoldComplete[mod_]} :> 
   HoldComplete@Mod[Total[x /. HoldComplete[a_] :> a], mod]

Examples

Mathematica graphics

Mathematica graphics

Circle-plus can be entered with ESC c+ ESC or \[CirclePlus]. A subscript can be entered with CTRL_ (underscore).

One could do something similar with \[CircleTimes] and modular multiplication.

$\endgroup$
1
$\begingroup$

Actually, I figured it out on my own. Wanted to post the solution in case anyone else ran into the same problem. It's just:

Mod[a + b, x]  

so that

Mod[1 + 1, 2]
0

Pretty straightforward actually.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.