Proper Treatment 正當作法/ blog/ posts/ Sudoku in SQL
標籤 Tags:
2008-08-17 19:19

I understand the attraction of writing a Sudoku solver in an unusual programming language such as SQL or XSLT. But I am surprised that a cursory Web search did not find any mention of what I take to be the simplest and most obvious way to solve a Sudoku puzzle in a single SQL statement, given the fundamental connection between relational databases (SQL) and logic programming (Datalog). That way is to treat each unknown digit as an output column, and the entire puzzle as one big join.

I wrote a short Haskell program to convert a Sudoku puzzle to a SQL query and solved it using PostgreSQL. The code assumes an existing table values with a single column v and 9 rows where v ranges from 1 to 9.

(Update: to clarify and emphasize my surprise, it is as if programmers left and right were coming up with solutions to the word problem

Alice had 3 cookies, then Bob gave her 5 more. How many cookies does she have now?

using arithmetic, such as by cubing 2 or by doubling 4, but not by adding 3 to 5.)