title: Primality test in python
body: <p>I'm trying to do a simple primality test in Python.</p>
  
  <p>Accoding to Wikipedia, a <a href="http://en.wikipedia.org/wiki/Primality_test" rel="noreferrer">primality test</a> is the following:</p>
  
  <blockquote>
    <p>Given an input number n, check whether any integer m from 2 to n − 1 divides n. If n is divisible by any m then n is composite, otherwise it is prime.</p>
  </blockquote>
  
  <p>I started with ruling out the even numbers - with the exception of 2 - as candidates to prime</p>
  
  <pre><code>def prime_candidates(x):
      odd = range(1, x, 2)
      odd.insert(0, 2)
      odd.remove(1)
      return odd
  </code></pre>
  
  <p>Then writing a function to check for primes, according to the rules above.</p>
  
  <pre><code>def isprime(x):
      for i in range(2, x-1):
              if x % i == 0:
                      return False
              else:
                      return True
  </code></pre>
  
  <p>And this is the main function, which iterates over a list of 8000 prime candidates and tests their primality</p>
  
  <pre><code>def main():
      end = 8000
      candidates = prime_candidates(end)
      for i in candidates:
              if isprime(i) and i &lt; end:
                      print 'prime found ' + str(i)
  </code></pre>
  
  <p>The problem is that the <code>isprime</code> function returns True for numbers that aren't primes.</p>