Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: sudoku solver
Replies: 2   Last Post: Sep 27, 2012 11:28 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Jure

Posts: 10
Registered: 9/25/12
sudoku solver
Posted: Sep 25, 2012 1:08 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hello, I'm trying to solve euler problem 96 - solve 50 sudokus. I have already written a solution in phyton and it works, but I have a problem in Mathematica.
The code works for some sudokus while for others it gives me an error just before it finishes.The error message is:
$RecursionLimit::reclim: Recursion depth of 256 exceeded. >>
General::stop: Further output of $RecursionLimit::reclim will be suppressed during this calculation. >>

examples:
\left(
\begin{array}{ccccccccc}
0 & 0 & 3 & 0 & 2 & 0 & 6 & 0 & 0 \\
9 & 0 & 0 & 3 & 0 & 5 & 0 & 0 & 1 \\
0 & 0 & 1 & 8 & 0 & 6 & 4 & 0 & 0 \\
0 & 0 & 8 & 1 & 0 & 2 & 9 & 0 & 0 \\
7 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 8 \\
0 & 0 & 6 & 7 & 0 & 8 & 2 & 0 & 0 \\
0 & 0 & 2 & 6 & 0 & 9 & 5 & 0 & 0 \\
8 & 0 & 0 & 2 & 0 & 3 & 0 & 0 & 9 \\
0 & 0 & 5 & 0 & 1 & 0 & 3 & 0 & 0
\end{array}
\right)

but the next one doesn't finish:
\left(
\begin{array}{ccccccccc}
2 & 0 & 0 & 0 & 8 & 0 & 3 & 0 & 0 \\
0 & 6 & 0 & 0 & 7 & 0 & 0 & 8 & 4 \\
0 & 3 & 0 & 5 & 0 & 0 & 2 & 0 & 9 \\
0 & 0 & 0 & 1 & 0 & 5 & 4 & 0 & 8 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
4 & 0 & 2 & 7 & 0 & 6 & 0 & 0 & 0 \\
3 & 0 & 1 & 0 & 0 & 7 & 0 & 4 & 0 \\
7 & 2 & 0 & 0 & 4 & 0 & 0 & 6 & 0 \\
0 & 0 & 4 & 0 & 1 & 0 & 0 & 0 & 3
\end{array}
\right)

This one gives an error when it tries to solve the last zero (position {9,8}). The weird thing is that if I try to solve position {9,8} first and then everything else in normal order, it just stops one earlier (position {9,7}.

My code is written below. I checked where it stops and it stops before the two for loops in sestopaj[..] module.
code:
main function:

\text{sestopaj}[\text{delno0$\_$}]\text{:=}\text{Module}[\{\text{delno}=\text{delno0},i=1,j=1,\text{k1},\text{z1},\text{mozne},d=0,\text{i1}=0,\text{j1}=0,a=0\},\{\text{i1}=0;\text{j1}=0;\text{If}[\text{resen}[\text{delno}],a=2;\text{Return}[]];\text{For}[i=1,i\leq 9,i\text{++},\text{For}[j=1,j\leq 9,j\text{++},\text{If}[\text{delno}[[i,j]]==0,\text{i1}=i;\text{j1}=j;\text{Break}[]];];\text{If}[\text{i1}\neq 0 \&\&\text{j1}\neq 0,\text{Break}[]];];\text{mozne}=\text{moznecifre}[\text{i1},\text{j1},\text{delno}];d=\text{Length}[\text{mozne}];\text{For}[\text{k1}=1,\text{k1}\leq 1d,\text{k1}\text{++},\text{delno}[[\text{i1},\text{j1}]]=\text{mozne}[[\text{k1}]];\text{Print}[\text{delno}];\text{z1}=\text{sestopaj}[\text{delno}];\text{If} [\text{z1}\neq \text{None},a=1;\text{Return}[] ]; ];\text{delno}[[\text{i1},\text{j1}]]=0;\};\text{If}[a==2,\text{Return}[\text{delno}]];\text{If}[a==1,\text{Return}[\text{z1}],\text{Return}[\text{None}]];]

auxiliary functions:

\text{cifre}[\text{a0$\_$},\text{b0$\_$},\text{delno3$\_$}]\text{:=}\text{Module}[\{a=\text{a0},b=\text{b0},\text{vse},c,e,l,o\},\{c=1;e=1;\text{If}[a>10 \|b>10,\text{Print}[a,b]];\text{vse}=\{1,2,3,4,5,6,7,8,9\};\text{For}[c=1,c<10,c\text{++},\text{If}[\text{delno3}[[c,b]]\neq 0 ,\text{vse}=\text{Complement}[\text{vse},\{\text{delno3}[[c,b]] \}] ];\text{If}[\text{delno3}[[a,c]]\neq 0, \text{vse}=\text{Complement}[\text{vse},\{\text{delno3}[[a,c]]\} ] ];];e=\text{IntegerPart}[(a-1)/3]*3+1;o=\text{IntegerPart}[(b-1)/3]*3+1;\text{For}[c=e,c<e+3,c\text{++},\text{For}[l=o,l<o+3,l\text{++},\text{vse}=\text{Complement}[\text{vse},\{\text{delno3}[[c,l]]\} ] ;]]\};\text{vse}]\text{moznecifre}[\text{f0$\_$},\text{g0$\_$},\text{delno1$\_$}]\text{:=}\text{Which}[\text{delno1}[[\text{f0},\text{g0}]]\neq 0,\{\},\text{delno1}[[\text{f0},\text{g0}]]==0, \text{cifre}[\text{f0},\text{g0},\text{delno1}]]\text{resen}[\text{delno2$\_$}]\text{:=}\text{Which}[\text{MemberQ}[\text{Flatten}[\text{delno2}],0],\text{False},\text{MemberQ}[\text{Flatten}[\text{delno2}],0]==\text{False},\text{True}];

I have tried changing a lot of things with no success. I'll appreciate any help. Thank you.



Date Subject Author
9/25/12
Read sudoku solver
Jure
9/26/12
Read Re: sudoku solver
Frank K
9/27/12
Read Re: sudoku solver
Jure

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.