1 / 3
Jun 2014

from math import sqrt
N = int(raw_input())
maximum,minimum = [int(x) for x in raw_input().split()]
def PrimeGenerator(x,y):
   primes = [2]
   for item in range(3,y,2):
      isprime = True
      a = sqrt(item) + 1
      for divisor in primes: 
         if divisor >= a:
            break
         if item % divisor ==0:
            isprime = False
            break
         else:
            primes.append(item)
   return primes
while N:
   N-=1
   prime_Number = PrimeGenerator(maximum,minimum)
   for number in prime_Number:
      print number

The output gives multiples of 9 too.. frowning

  • created

    May '14
  • last reply

    Jun '14
  • 2

    replies

  • 247

    views

  • 3

    users

def PrimeGenerator(x,y):
   primes = [2]
   for item in range(3,y,2):
      isprime = True
      a = sqrt(item) + 1
      for divisor in primes: 
         if divisor >= a:
            break
         if item % divisor ==0:
            isprime = False
            break
         else:
            primes.append(item)
   return primes

I don't use python, but it looks like if the item is not divisible by 2 you append it to primes. I suspect that there should be an if isPrime = true at some point in this code, correct?

1) x isn't used the code, it should be for the next step in the writing, I suppose.
2) Unused isprime variable, it should be. (only set)
3) We have to go to y include, so to y+1 (exclude)
4) The code fail for y<2, I suppose you know that, not a serious issue
5) three spaces for indentation is not common, I use tab, (or 2 or 4 spaces).

Here is a code that do the job, with minimal changes, hope it will be helpful.
Then some more modifications, light...

BR
F

from math import sqrt
def PrimeGenerator(y):
   primes = [2]
   for item in range(3,y+1,2):
      isprime = True
      a = sqrt(item) + 1
      for divisor in primes:
     if divisor >= a:
        break
     if item % divisor ==0:
        isprime = False
        break
  if isprime:
        primes.append(item)
   return primes
for y in range(100):
   print(y, PrimeGenerator(y))
# A variation, without the use of sqrt
def primes(n):
	if n<2:
		return []
    prime=[2]

for i in range(3, n+1, 2):
	for p in prime:
		if p*p>i: # then i is a prime number
			prime.append(i)
			break
		if i%p==0: # then i is a composed number
			break

return prime
for n in range(100):
	print(n, primes(n))
    if n>1: assert primes(n) == PrimeGenerator(n) , ("test fail for", n)