title: Problem with Euler 27
body: <blockquote>
  <pre><code>Euler published the remarkable quadratic formula:
  
  n² + n + 41
  
  It turns out that the formula will produce 40 primes for the consecutive
  </code></pre>
    
    <p>values n = 0 to 39. However, when n =
    40, 40^(2) + 40 + 41 = 40(40 + 1) + 41
    is divisible by 41, and certainly when
    n = 41, 41² + 41 + 41 is clearly
    divisible by 41.</p>
  
  <pre><code>Using computers, the incredible formula n² − 79n + 1601 was
  </code></pre>
    
    <p>discovered, which produces 80 primes
    for the consecutive values n = 0 to
    79. The product of the coefficients, −79 and 1601, is −126479.</p>
  
  <pre><code>Considering quadratics of the form:
  
  n² + an + b, where |a| &lt; 1000 and |b| &lt; 1000
  
  where |n| is the modulus/absolute value of n
  e.g. |11| = 11 and |−4| = 4
  
  Find the product of the coefficients, a and b, for the
  </code></pre>
    
    <p>quadratic expression that produces the
    maximum number of primes for
    consecutive values of n, starting with
    n = 0.</p>
  </blockquote>
  
  <p>This is the problem for <a href="http://projecteuler.net/index.php?section=problems&amp;id=27" rel="nofollow">Euler 27</a>.</p>
  
  <p>I have attempted a solution for trying to find the equation n^2 + n + 41 to see if my logic is correct then I will attempt to see if it works on the actual problem. Here is my code (I will place comments explaining the whole program also, I would start reading from the int main function first) just make sure to read the comments so you can understand my logic:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  bool isPrime(int c) {
      int test;
      //Eliminate with some simple primes to start off with to increase speed...
      if (c == 2) {
          return true;
      }
      if (c == 3) { 
          return true;
      }
      if (c == 5) { 
          return true;
      }
      //Actual elimination starts here.
      if (c &lt;= 1 || c % 2 == 0 || c % 3 == 0 || c % 5 == 0) {
          return false;
      }
      //Then using brute force test if c is divisible by anything lower than it except 1
      //only if it gets past the first round of elimination, and if it doesn't
      //pass this round return false.
      for (test = c; test &gt; 1; test--) {
          if (c % test == 0) {
              return false;
          }
      }
      //If the c pasts all these tests it should be prime, therefore return true.
      return true;
  }
  
  int main (int argc, char * const argv[]) {
      //a as in n^2 + "a"n + b
      int a = 0;
      //b as in n^2 + an + "b"
      int b = 0;
      //n as in "n"^2 + a"n" + b
      int n = 0;
      //this will hold the result of n^2 + an + b so if n = 1 a = 1
      //and b = 1 then c = 1^2 + 1(1) + 1 = 3
      int c = 0;
      //bestChain: This is to keep track for the longest chain of primes 
      //in a row found.
      int bestChain = 0;
      //chain: the current amount of primes in a row.
      int chain = 0;
      //bestAB: Will hold the value for the two numbers a and b that
      // give the most consecutive primes.
      int bestAB[2] = { 0 };
      //Check every value of a in this loop
      for (a = 0; a &lt; 40; a++) {
          //Check every value of b in this loop.
          for (b = 0; b &lt; 42; b++) {
              //Give c a starting value
              c = n*n + a*n + b;
              //(1)Check if it is prime. And keep checking until it is not
              //and keep incrementing n and the chain. (2)If it not prime then che
              //ck if chain is the highest chain and assign the bestChain
              // to the current chain. (3)Either way reset the values
              // of n and chain.
              //(1)
              while (isPrime(c) == true) {
                  n++;
                  c = n*n + a*n + b;
                  chain++;
              }
              //(2)
              if (bestChain &lt; chain) {
                  bestChain = chain;
                  bestAB[0] = a;
                  bestAB[1] = b;
                  chain = 0;
                  n = 0;
              }
              //(3)
              else {
                  n = 0;
                  chain = 0;
              }
          }
      }
      //Lastly print out the best values of a and b.
      cout &lt;&lt; bestAB[0] &lt;&lt; " " &lt;&lt; bestAB[1]; 
      return 0;
  }
  </code></pre>
  
  <p>But, I get the results 0 and 2 for a and b respectively, why is this so? Where am I going wrong? If it is still unclear just ask for more clarification on a specific area.</p>