Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What I would do, is actually use binary bits for storing actual values as another answer suggested. That allows to do efficient checks and in general might lend Sudoku itself to more mathematical(=efficient and shorter) solution (just my impression, I have not researched this).</p> <p>Basically, you represent the numbers in squares not with digits, but with powers of 2</p> <pre><code>"1" = 2^0 = 1 = 000000001 "2" = 2^1 = 2 = 000000010 "3" = 2^2 = 4 = 000000100 "4" = 2^3 = 8 = 000001000 ... etc up to "9" = 2^8 = 256= 100000000 </code></pre> <p>this way, you can simply add contents' of single squares to find out what numbers are missing in a 3x3 or a row or any other subset of sudoku, like this:</p> <pre><code>// shows the possibles for 3x3 square number 1 (00-22) $sum=0; for ($i=0; $i&lt; 3; $i++) for ($j=0; $j &lt; 3; $j++) $sum += $square["${i}${j}"]-&gt;number $possibles = $sum ^ 511 // ^ stands for bitwise XOR and 511 is binary 11111111 </code></pre> <p>now the $possibles contains "1" in bit positions of digits that are possible in this square and you can do bitwise operations with the results for other squares to match them together, like this:</p> <p>eg. let's say: </p> <pre><code>$possibles1 = 146 // is binary 100100101, //indicating that this row or 3x3 square has place for "9", "6", "3" and "1" $possibles2 = 7 // is binary 000000111, indicating it has place for "3", "2" and "1". // so: $possibles1 &amp; $possibles2 // bitwise AND, will show binary 101 saying that "3" and "1" is unfilled in both bloces $possibles1 | $possibles2 // bitwise OR will give that in total it is possible to use "9", "6", "3", "2" and "1" in those two squares together </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload