title: Crash to desktop in simple math operation
body: <p>Warning: Spoilers for Project Euler question nr. 3</p>
  
  <p>I have code that crashes to desktop and no debugger available. To help pinpoint the problem, I've thrown in a few std::cout statements and recompiled.</p>
  
  <p>The code:</p>
  
  <pre><code>/*The prime factors of 13195 are 5, 7, 13 and 29.
  
  What is the largest prime factor of the number 600851475143 ?*/
  
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;cmath&gt;
  
  int main(){
  
      const long long target = 600851475143;
      double squareRootOfTarget = sqrt(target);
      long longSquareRootOfTarget = floor(squareRootOfTarget+1);
  
      std::vector&lt;long&gt; primes;
      primes.push_back(2);
  
      bool test = 0;
  
      for( long i = 3; i &lt; longSquareRootOfTarget; i++){
  
          std::cout &lt;&lt; "\n\nTesting: " &lt;&lt; i &lt;&lt; " -";
  
          for( int j = 0;     j&lt;(primes.size()+1);     j++){
  
              std::cout &lt;&lt; " " &lt;&lt; j;
              if (0 == i%primes[j])
              {
                  test = 1;
              }
              std::cout &lt;&lt; " " &lt;&lt; j;
          }
  
          std:: cout &lt;&lt; "\n Finished testing " &lt;&lt; i;
  
          if (0 == test) {
  
              primes.push_back(i);
  
              std::cout &lt;&lt; "\n" &lt;&lt; primes.back() &lt;&lt; " is a prime.";
          }
  
  
  
          test = 0;
      }
  
  
      return 0;
  }
  </code></pre>
  
  <p>The problem: On iteration i = 23, it prints the expected:</p>
  
  <pre><code>Testing: 23 - 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8
   Finished testing 23
  23 is a prime.
  </code></pre>
  
  <p>Then, on iteration i = 24, it suddenly fails:</p>
  
  <pre><code>Testing: 24 - 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9
  *crash*
  </code></pre>
  
  <p>This presumably means the error is in the conditional <code>if (0 == i%primes[j])</code> which SHOULD expand into the simple <code>if (0 == 24%23)</code> and evaluate to false but apparently that doesn't happen. (It could, of course, also happen in the <code>test = 1;</code> assignment but that would surprise me.)</p>
  
  <p>Any idea what could be going wrong?</p>