# Creating a grid of coloured points

I wish to create a grid of 31x31 points with coordinates of the form $(k/30,n/30)$ within the unit square, and give each point a colour based on the 2-adic value of both its coördinates.

I plan to use Wolfram Mathematica, as this program has a simple function to calculate the 2-adic value, but I do not know how to generate a grid in which I can assign colours to the points. If we write $v(x)$ as the 2-adic value of $x$, the colouring should be as follows:

$(x,y)$ is coloured:

• Blue if $v(x) \geq v(y)$ and $v(x) \geq v(1)=1$
• Green if $v(x) < v(y)$ and $v(y) \geq v(1)=1$
• Red if $v(x) < v(1)=1$ and $v(y) < v(1)=1$

Can someone help me?

• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Sep 26 '15 at 14:00
• Please add any code you have tried. – bbgodfrey Sep 26 '15 at 14:01
• Take a look at ArrayPlot. – gwr Sep 26 '15 at 14:02

I don't know much about 2-adic values, hence I took the code for it from this mathworld article. Here are a couple of solutions to do it:

## Using Graphics

PadicNorm[x_Integer, p_Integer?PrimeQ] := p^(-IntegerExponent[x, p])
Graphics[{
Table[{PointSize@0.016,
Which[
v[k/30] >= v[n/30] && v[k/30] >= 1, Blue,
v[k/30] < v[n/30] && v[n/30] >= 1, Green,
v[k/30] < 1 && v[n/30] < 1, Red
],
Point[{k/30, n/30}]
},
{k, 0, 30}, {n, 0, 30}
]
}
]


Which produces the following:

## Using squares instead of points

Using appropriate substitutions on a ConstantArray and selectively changing the background color:

PadicNorm[x_Integer, p_Integer?PrimeQ] := p^(-IntegerExponent[x, p])
vx[j_] := PadicNorm[(j - 1)/30, 2]
vy[i_] := PadicNorm[(31 - i)/30, 2]
ReplacePart[
ConstantArray[0, {31, 31}],
{{i_, j_} :>
Item[" ", Background -> Blue] /; vx[j] >= vy[i] && vx[j] >= 1,
{i_, j_} :>
Item[" ", Background -> Green] /; vx[j] < vy[i] && vy[i] >= 1,
{i_, j_} :> Item[" ", Background -> Red] /; vx[j] < 1 && vy[i] < 1}
] // Grid


The functions vx and vy serve to easily convert between the numbering of the elements of a matrix and the required cartesian way of ordering coordinates. Here is the output:

## Output squares using Graphics

Slight modification of the previously used code with Graphics:

PadicNorm[x_Integer, p_Integer?PrimeQ] := p^(-IntegerExponent[x, p])
Graphics[{
Table[
{
Which[
v[k/30] >= v[n/30] && v[k/30] >= 1, Blue,
v[k/30] < v[n/30] && v[n/30] >= 1, Green,
v[k/30] < 1 && v[n/30] < 1, Red
],
Rectangle[{k/30, n/30}, {(k + 1)/30, (n + 1)/30}]
},
{k, 0, 30}, {n, 0, 30}
]
}
]


And this is the output:

• Thank you very much, this is what I needed! Yet, is there a way to, instead of colouring adjacent squares, colour loose points in the unit square? – Pepijn Sep 26 '15 at 15:20
• @Pepijn of course. Sorry I didn't understand that that's what you needed. See the edit. – glS Sep 26 '15 at 15:26
• Thank you very much, this is exactly what I needed! – Pepijn Sep 26 '15 at 15:38