The purpose of this challenge is to graphically depict a walk on the plane, where the direction of each step \$k\$ is determined by the primality of \$k\$ and the parity of its binary expansion. Specifically,
- Initial direction is fixed, say North.
- All steps have the same length.
- The direction of step \$k\$ can be North, West, South or East, and is determined as follows:
- If \$k\$ is not prime, the direction does not change.
- If \$k\$ is prime and the binary expansion of \$k\$ has an even number of ones, turn right.
- If \$k\$ is prime and the binary expansion of \$k\$ has an odd number of ones, turn left.
As a worked example, assume that the initial direction is North. The first steps are:
- \$k=1\$ is not prime. So we move one step in the current direction, which is North.
- \$k=2\$ is prime, and its binary expansion,
10
, has and odd number of ones. So we turn left, and are now facing West. We move one step in that direction. - \$k=3\$ is prime, and its binary expansion,
11
, has and even number of ones. So we turn right, and are now facing North. We move one step in that direction. - \$k=4\$ is not prime. So we move one step in the current direction, which is North.
The challenge
Input: positive integer \$N\$.
Output: plot of the \$N\$-step walk as defined above.
Additional rules
- The initial direction can be freely chosen (not necessarily North), but should be the same for all \$N\$.
- The turning rule can be the opposite to that described above, that is, turn right for odd parity and left for even; but it has to be the same for all \$N\$.
- The output has to be a graphical depiction of the walk. For instance:
- The walk can be drawn with line segments.
- The visited points can be shown with a marker, such as a dot; with or without connecting line segments.
- A two-colour raster image can be provided, with one colour corresponding to visited points and another for non-visited.
- The scales of the horizontal and vertical axes need not be the same. Also axis labels and similar elements are optional. As long as the walk can be clearly seen, the plot is valid.
- Note that some points are visited more than once. The plot is not sensitive to this. For instance, if line segments are shown in the plot, each unit segment is displayed the same no matter how many times it has been traversed.
- The code should work for any
N
given unlimited resources. It is acceptable if in practice it fails for largeN
due to time, memory or data-type limitations. - Input and output are flexible as usual. In particular, any of the standard means for outputting images can be used.
- The shortest code in bytes wins.
Test cases
The following plots use North as initial direction; even parity turns right; and the walk is depicted with line segments.
N = 7
:
N = 3000
:
N = 20000
:
N = 159000
:
N = 1200000
:
N = 11000000
:
[graphical-output]
is allowed? Any reason in particular to disallowed ASCII output, like my now deleted Charcoal answer? \$\endgroup\$