print_sudoku(L, N) :- nl, print_sudoku(L, N, 1, 1), nl, nl.

print_sudoku(L, N, X, Y) :- member([S,X,Y],L), write(S),
			    move_to_next_square(L, N, X, Y).

chkForExtraSpace(N, X) :- N12 is round(sqrt(N)), XModN12 is (X mod N12), XModN12 =:= 0, write(' '), !.
chkForExtraSpace(_, _).

chkForExtraLine(N, Y) :- N12 is round(sqrt(N)), YModN12 is (Y mod N12), YModN12 =:= 0, nl, !.
chkForExtraLine(_, _).

move_to_next_square(L, N, R, C) :- C < N, chkForExtraSpace(N, C),
				   C1 is C + 1, write(' '), print_sudoku(L, N, R, C1).
move_to_next_square(L, N, R, C) :- C =:= N, R<N, chkForExtraLine(N, R),
				   R1 is R + 1, nl, print_sudoku(L, N, R1, 1).
move_to_next_square(_, N, N, N).