user11001277
user11001277

Reputation:

Python Prime number list generator errors

This code begins at 5, and lists the following prime numbers up to your choice, which in this case, is the following 17 prime numbers. When I run it, 25 and 49 are printed. Why are they not filtered out?

start = 5
number = 1
divisor = 3
upper = start - 2
doc = open("hey.txt", "w")
while number <= 17:
    if start % divisor == 0:
        start = start + 2
        divisor = 3
    elif divisor == upper:
        doc.write(str(start))
        doc.write(", ")
        number = number + 1
        start = start + 2
        divisor = 3
    else:
        divisor = divisor + 2


hey.txt: 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53, 

Upvotes: 0

Views: 70

Answers (4)

DirtyBit
DirtyBit

Reputation: 16782

Cleaner and shorter:

lower = 5
upper = 50

print("Prime numbers between {} and {} are: ".format(lower, upper))

doc = open("hey.txt", "w")
for num in range(lower,upper + 1):
   # prime numbers are greater than 1
   if num > 1:
       for i in range(2,num):
           if (num % i) == 0:
               break
       else:
           print(num)
           doc.write(str(num))
           doc.write(",")

OUTPUT:

Prime numbers between 5 and 50 are: 
5,7,11,13,17,19,23,29,31,37,41,43,47,

Upvotes: 0

ddor254
ddor254

Reputation: 1628

you need to update your upper variable, i will explain:

when you write your start number to the file , it means that you found this number to be a prime number, hence you need to update the upper variable to be the new start -2 value , since you increased start. so your function should look like:

start = 5
number = 1
divisor = 3
upper = start - 2
doc = open("hey.txt", "w")
while number <= 17:
    if start % divisor == 0:
        start = start + 2
        divisor = 3
    elif divisor == upper:
        doc.write(str(start))
        doc.write(", ")
        number = number + 1
        start = start + 2
        divisor = 3
        upper = start - 2 # this is the line you forgot.
    else:
        divisor = divisor + 2

Upvotes: 1

Superluminal
Superluminal

Reputation: 977

You got allready an answer on you question. But take a look at this question. This is a way faster approach to do primes Fast primes with python:

def primes2(n):
    """ Input n>=6, Returns a list of primes, 2 <= p < n """
    n, correction = n-n%6+6, 2-(n%6>1)
    sieve = [True] * (n//3)
    for i in range(1,int(n**0.5)//3+1):
      if sieve[i]:
        k=3*i+1|1
        sieve[      k*k//3      ::2*k] = [False] * ((n//6-k*k//6-1)//k+1)
        sieve[k*(k-2*(i&1)+4)//3::2*k] = [False] * ((n//6-k*(k-2*(i&1)+4)//6-1)//k+1)
    return [2,3] + [3*i+1|1 for i in range(1,n//3-correction) if sieve[i]]

True, its not really readable. But first olways look if other people did already what you a trying to do. Python is a language that has a huge community and its benefits are a ton of libraries and a ton of developed algorithms/programs that do probably what you want ot do in a cleaner and faster way than you will do it by yourself. So enjoy the benefit of the community.)

Upvotes: 0

G. B.
G. B.

Reputation: 628

Probably because you don't increase upper. So the maximal divisor you test is 3.

Upvotes: 0

Related Questions