Skip to main content Skip to navigation

Sudoku

It appears Sudoku is sweeping the nation and even I have taken to doing the odd puzzle. Whilst solving the puzzles it became apparent that they can be solved using software and indeed Peter Cock who is also on the MOAC programme has written a program to solve puzzles.

Both Peter and Neil Stewart who also wrote a program to solve puzzles state they took about three hours to come up with a solution. It seemed to me this was rather a long time and the problem could be solved in only a few lines of code so I wrote a solution in Java to demonstrate this.

This solution was written from a naive perspective. I did not consult any sites or books explaining ways of solving puzzles. I chose to write a fully recursive functional program to do the job. Whilst it might not be optimal the code is somewhat more elegant and much shorter than some of the solutions I've seen. It is also exhaustive and therefore guarantees it will find a solution should one exist.

I wrote the program to demonstrate the elagance of functional programs and how you can write them in Java. Hopefully you will notice there is no use of for or do loops (except in the start up code but that is just to do some I/O) and only a few simple functions are required to find a solution.