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 { }.


closed as off-topic by m_goldberg, Oleksandr R., Kuba, Öskå, Karsten 7. Jan 26 '15 at 3:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Oleksandr R., Kuba, Öskå, Karsten 7.
If this question can be reworded to fit the rules in the help center, please edit the question.


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]


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.


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]

Pretty straightforward actually.


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