Home | What is Sudoku? | Su Doku Shop | What are Kakuro and Hanjie? | Recommended free games | On-line Sudoku | Math | Image Su Do Ku | Techniques | Our Sudoku Solver | Sudoku top 50 | Links
Google

123 Sudoku Solver in R

First edit the Sudoku grid below in order to solve your own puzzle. Note `0' means missing value. Please edit carefully - you need to keep the commas here!

Then press Go! (once only) Be patient, a new screen appears with `R' at the top and WAIT until it says `Program Output' (you need to wait several seconds or longer).

then scroll to the end of the page to see three plots: the puzzle, the number of possibilities for each cell, and the solution. This program is written in R and makes use of the Rcgi web interface . The software is free.

Details of the backtracking algorithm are given below (after the code).


Details of the program

It is reasonably easy to write a program to solve Sudoku using a backtracking algorithm.

What is the back-tracking algorithm?

1. First of all check each square in turn for the number of possible numbers that could be in that position, by eliminating the other numbers that are given in the same row, column and 3x3 grid.

2. Then starting at the first unknown square we'll try with the lowest available number that is allowed.

3.Move on to the next square then we'll try with the lowest available number...and keep doing this.

4. If you hit a problem and there are no available numbers then move back a square [i.e. you must `back-track'], and increase that digit to be the next lowest available number. Go to step 3.

5. Eventually you should pass through all the squares in the grid - and `voila' you have the solution!

Note you may end up having to back-track all the way back to the beginning several times! However, a computer can do this easily and reasonably quickly.

We wrote this program in R fairly quickly and it seems to do the job. Making it work faster is more of a challenge - that's left for another day. Also, working out how many solutions a Sudoku grid has is not so straightforward - again on the back burner for now.

If you're interested in using this more than a few times then you ought to download R (see cran.r-project.org ) and install it on your machine (it's free!), then source in this R code . There is absolutely no warranty and the Licence is GPL2. We should warn you that you probably need to be familiar with R to do this - or at least be a bit of a boffin. There's another challenge for you!

One nice by-product is that the program gives you the number of possibilities at the outset for each square, which can be a helpful way to start a difficult puzzle if you're stuck.

All puzzles on this site have been checked using this program.

Google