I did a sudoku puzzle for the first time in a long time this weekend, and it got me thinking about a few questions around the number of possible puzzles, etc.
The rules of Sudoku are pretty simple. For a 9 x 9 grid, fill it in so each row includes the digits 1 – 9 and each column includes the digits 1 – 9. Additionally, the grid is broken up into nine 3 x 3 sub-grids. Each character can only appear once in each grid.
Lazy Sudoku
Lazy Sudoku is a super fun game I invented. You start with Sudoku, except rows don’t need to be unique. Nor columns… Nor sub-grids.
How many variations are possible if the only rules are to fill in the N x N grid with the digits 0 – N?
This one’s pretty easy, it’s just an N^2 digit number. For a 1 x 1 grid, you can put 1. For a 2 x 2 grid, you can put 1111, 1112, 1113, …4443, 4444. For a standard 9 x 9 grid, you can fill it from 1{81 times} – 9{81 times}. Generally, to figure out how many values can be represented by a number in a positional number system (what we use, not Roman Numerals), you take the number of possible characters and raise it to the number of spaces you can fill in. So for a three digit, base-10 number, it’s 10 ^ 3 or 1000.
So for our N ^ 2 digit number, with N possible characters, it’s N ^ (N ^ 2). That would mean our Lazy Sudoku puzzle has 9 ^ 9 ^ 2 possibilities, also known as 196627050475552913618075908526912116283103450944214766927315415537966391196809
How can you build a Lazy Sudoku puzzle?
I mean.. you can just do [random() for i in range(N)]. If you want to get a tiny bit fancier (but more verbose and generally worse), you could do:
def createLazySudoku(puzzleSize=81, charSet=range(1, 10)):
char = random.choice(charSet)
if puzzleSize == 1:
return f'{char}'
return f'{char}{createLazySudoku(puzzleSize - 1, charSet)}'