0

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, 
  • 1
    it looks like you filter out just numbers that are multiples of 2 or 3, e.g. you also have 35 = 5 * 7. With more numbers you will get even more multiples of other bigger prime numbers. – buran Feb 19 '19 at 09:27
  • Is the intention to print onloy the prime numbers? – DirtyBit Feb 19 '19 at 09:28
  • If your intentions is prime number generations - look at [Sieve_of_Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) – Alex Yu Feb 19 '19 at 09:30

4 Answers4

1

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
ddor254
  • 1,570
  • 1
  • 12
  • 28
0

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

G. B.
  • 528
  • 2
  • 15
0

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.)

Superluminal
  • 947
  • 10
  • 23
0

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,
DirtyBit
  • 16,613
  • 4
  • 34
  • 55