How can I construct a colored grid in Mathematica representing the remainder of $x^{y}$ modulo a given prime $p$?(with coordinates) As such:
2 Answers
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.
-
1$\begingroup$ One can also use
Outer
instead ofTable
and applyMod
in a vectorized way:remainders = Mod[Outer[Power, Range[p - 1], Range[p - 1]], p]
. $\endgroup$ Dec 3, 2018 at 23:45
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]]
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}}]]
-
$\begingroup$ Thanks! But how do I make it work for a generic given prime $p$? $\endgroup$ Dec 4, 2018 at 8:12
-