Let's create a N×N grid of spaces and underscores that can be used to visually determine if a number is prime. (N may be any positive integer.)
This grid has three simple rules:
- The nth column contains the repeated pattern of n - 1 underscores followed by one space. This pattern starts at the first row and is stopped, possibly mid-pattern, at row N. (Rows and columns are 1-indexed.)
- The first column is replaced with all underscores instead of all spaces.
- If a space occurs somewhere the row index equals the column index it is replaced with an underscore.
Example: N = 10
1
1234567890 <-- column indices
1__________
2__________
3__________
4_ ________
5__________
6_ _______
7__________
8_ _ ______
9__ _______
10_ __ _____
^ row indices
The indices are just for clarity. The plain grid itself (what your program must output) is:
__________
__________
__________
_ ________
__________
_ _______
__________
_ _ ______
__ _______
_ __ _____
Notice that:
- The first column is all underscores.
- The second column goes underscore space, underscore space, etc., except for the underscore on row 2.
- The third column goes underscore underscore space, underscore underscore space, etc., except for the underscore on row 3.
- etc.
Also notice that besides 1, only prime numbered rows have underscores in every column.
Since underscores span the entire font width, each prime numbered row forms a continuous solid line. So checking if a number is prime or not is quite easy visually; just check if its line is solid across all columns. (In fact looking up to the square root of the row index suffices but outputting that grid seems less elegant.)
Program
Write a program that will draw these grids given N via stdin (or closest alternative). Output goes to stdout (or closest alternative) and should only contain spaces, underscores, and newlines, with an optional trailing newline.
The shortest code wins.
n
th line, make thek
th character a space ifk
is a divisor ofm
that isn't 1 orm
. \$\endgroup\$