title: Project Euler #27 Python
body: <p>Project Euler Problem #27 is as follows:</p>
  
  <blockquote>
    <p>Euler discovered the remarkable quadratic formula:</p>
    
    <p>n² + n + 41</p>
    
    <p>It turns out that the formula will produce 40 primes for the
    consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 =
    40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41² +
    41 + 41 is clearly divisible by 41.</p>
    
    <p>The incredible formula  n² − 79n + 1601 was discovered, which produces
    80 primes for the consecutive values n = 0 to 79. The product of the
    coefficients, −79 and 1601, is −126479.</p>
    
    <p>Considering quadratics of the form:</p>
    
    <p>n² + an + b, where |a| &lt; 1000 and |b| &lt; 1000</p>
    
    <p>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 quadratic
    expression that produces the maximum number of primes for consecutive
    values of n, starting with n = 0.</p>
  </blockquote>
  
  <p>And this is my solution:</p>
  
  <pre><code>from math import sqrt, fabs
  
  def eSieve(rnge):
      rootedrange = int(sqrt(rnge))
      mydict = dict([(_, True) for _ in range(2, rootedrange)])
      for i in range(2, rootedrange):
          if mydict[i] == True:
              for j in range(i**2, rnge, i):
                  mydict[j] = False
      mylist = []
      for key in mydict.keys():
          if mydict[key] is True:
              mylist.append(key)
      return mylist
  
  primes = eSieve(87400)
  
  def isPrime(n):
      i = 0
      while primes[i] &lt;= n:
          if primes[i] == n: return True
          i+=1
      return False
  
  arange = 0
  brange = 0
  nrange = 0
  for a in range(-1000, 1001):
      for b in range(-1000, 1001):
          n = 0
          formula = n*n + a*n + b
          print(formula)
          while(isPrime(fabs(formula))):
              n+=1
  
          if n &gt; nrange:
              arange = a
              brange = b
              crange = c
  
  print(arange * brange)
  </code></pre>
  
  <p>I do not know why is it continuously throwing this error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "D:\Programming\ProjectEuler\p27.py", line 33, in &lt;module&gt;
      while(isPrime(fabs(formula))):
    File "D:\Programming\ProjectEuler\p27.py", line 20, in isPrime
      while primes[i] &lt;= n:
  IndexError: list index out of range
  </code></pre>
  
  <p>Can anyone tell where and how is my program getting out of lists range? It's very abnormal. Why is this happening?</p>