1
$\begingroup$

How can I construct a colored grid in Mathematica representing the remainder of $x^{y}$ modulo a given prime $p$?(with coordinates) As such:

enter image description here

$\endgroup$

2 Answers 2

3
$\begingroup$

You can try:

p = 7;
remainders = Table[Mod[x^y, p], {x, 1, 6}, {y, 1, 6}];
MatrixPlot[remainders]

You can look at the documentation on ArrayPlot to get an idea of how you can format the colours and grid lines to match whatever you have in mind.

$\endgroup$
1
  • 1
    $\begingroup$ One can also use Outer instead of Table and apply Mod in a vectorized way: remainders = Mod[Outer[Power, Range[p - 1], Range[p - 1]], p]. $\endgroup$ Dec 3, 2018 at 23:45
1
$\begingroup$

Edit:

p = 11;
remainders = Mod[Outer[Power, Range[p - 1], Range[p - 1]], p];
color = Most[
  Hue[#] & /@ 
   Subdivide[p - 1]]; (*or use color=ColorData["Rainbow"]/@Subdivide[p-2]*)
Legended[ArrayPlot[remainders, Frame -> True, FrameTicks -> All, 
  FrameLabel -> {Style["x", Black, 20], Style["y", Black, 20]}, 
  FrameTicksStyle -> Directive[Black, 20], 
  PlotLabel -> Framed@Style["p=" <> ToString[p], Black, 20, Bold], 
  ColorRules -> Thread[Range[p - 1] -> color], Mesh -> All], 
 SwatchLegend[color, Range[p - 1], LegendMarkerSize -> 20]]

enter image description here

Original answer:

Let's use Henrik's solution:

p = 7;
remainders = Mod[Outer[Power, Range[p - 1], Range[p - 1]], p];

color = {Red, Green, Brown, Purple, Darker[Green, 0.8], Yellow};
Legended[ArrayPlot[remainders, Frame -> True, FrameTicks -> All, 
  FrameLabel -> {Style["a", Black, 20], Style["k", Black, 20]}, 
  FrameTicksStyle -> Directive[Black, 20], 
  PlotLabel -> Framed@Style["p=7", Black, 20, Bold], 
  ColorRules -> Thread[Range@6 -> color], Mesh -> All], 
 SwatchLegend[color, Range@6, LegendMarkerSize -> {{30, 30}}]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks! But how do I make it work for a generic given prime $p$? $\endgroup$ Dec 4, 2018 at 8:12
  • $\begingroup$ @James See edit.. $\endgroup$ Dec 4, 2018 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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