Note that there are some explanatory texts on larger screens.

plurals
  1. POSudoku checker algorithm in C++
    primarykey
    data
    text
    <p>I'm currently working on an algorithm to find all numbers with 9 digits using numbers 1-9 without any repeats. I'm testing a theory I have that filtering numbers as such will make for a more efficient sudoku checker.</p> <p>The code that I implemented does the following. It uses a for loop for places 1-9 in a number, such that (a)(b)(c)(d)(e)(f)(g)(h)(i) = #########. </p> <p>My theory is that by checking if the sum of the numbers (a-i) is equal to 45, that the product of a through i is equal to 9! and that the sum of the inverses of a-i is equal to roughly 2.828968 (or 1 + 1/2 + 1/3 ... 1/9)</p> <p>The issue is that after I filter the 9-digit numbers by the sum of the inverses of a-i, the count of possible 9-digit numbers predicted is less than 9! (the actual amount of possible numbers). I'm not sure why it's filtering so much, but the numbers that it does catch do not have any repeats (which is good). </p> <p>My thoughts are that the way I am playing with doubles is messing up the algorithm.</p> <p>Here is my code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;iomanip&gt; using namespace std; int main() { int product; int sum; int count=0; double inverseSum; double correctInverseSum=(1.0/1.0)+(1.0/2.0)+(1.0/3.0)+(1.0/4.0)+(1.0/5.0)+ (1.0/6.0)+(1.0/7.0)+(1.0/8.0)+(1.0/9.0); for(double a=1.0; a&lt;10.0; a++){ for(double b=1.0; b&lt;10.0; b++){ for(double c=1.0; c&lt;10.0; c++){ for(double d=1.0; d&lt;10.0; d++){ for(double e=1.0; e&lt;10.0; e++){ for(double f=1.0; f&lt;10.0; f++){ for(double g=1.0; g&lt;10.0; g++){ for(double h=1.0; h&lt;10.0; h++){ for(double i=1.0; i&lt;10.0; i++){ product=a*b*c*d*e*f*g*h*i; sum=a+b+c+d+e+f+g+h+i; if(product==9*8*7*6*5*4*3*2*1 &amp;&amp; sum==45){ inverseSum=(1.0/a)+(1.0/b)+(1.0/c)+(1.0/d)+ (1.0/e)+(1.0/f)+(1.0/g)+(1.0/h)+(1.0/i); if(inverseSum==correctInverseSum) { count++; } } } } } } } } } } } cout&lt;&lt;"This is the count:"&lt;&lt;count&lt;&lt;endl; return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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