0

I'm trying to write a program that picks out the primes in a range from 3 to 9. Here's my code:

primes_list = []
number = 3
while number > 2 and number < 10:
    for n in range(2, number):
        if number % n == 0:
            break
        number += 1
    else:
        primes_list.append(number)
print primes_list

This seems to be an infinite loop but that's just a guess because the output never shows up. If it is an infinite loop, why?

Also I was wondering if there's some sort of list comprehension that can pick out only prime numbers? Maybe list comprehensions is the more efficient way to go? only I haven't figure out how to just filter the primes.

Any help or comment is deeply appreciated.

jason adams
  • 535
  • 1
  • 13
  • 28
  • 1
    Your indentation looks wrong between the `if` and `else`. They should be at the same level. Edit: Disregard this comment. I stand corrected. – Gareth Mar 25 '14 at 15:08
  • Yep, you `number +=1` is misplaced. Try to see what happen. – fredtantini Mar 25 '14 at 15:08
  • 1
    @Gareth `for` loops can take an `else` clause, which is executed if the loop exits early (e.g., with `break`, instead of exhausting the iterable). – chepner Mar 25 '14 at 15:09
  • I think you should add 1 to the number in the else case as well. – Dschoni Mar 25 '14 at 15:09
  • 3
    @Gareth no it's probably using the else clause of the for loop. The number += 1 is misplaced though, should be on the same column (and after) the else – Ant Mar 25 '14 at 15:09
  • @chepner My bad, thanks for pointing that out. – Gareth Mar 25 '14 at 15:09
  • 1
    @Gareth this `else` belongs to `for`, see http://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops – m.wasowski Mar 25 '14 at 15:10
  • http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python if you need more than the primes between 3 and 9. – Dschoni Mar 25 '14 at 15:13
  • I purposely put the {else} outside of the {for} loop in order for it to loop through the whole range before executing the else. Because if I put the {if} and {else} at the same level 9 would get printed. – jason adams Mar 25 '14 at 15:14
  • @Ant thanks! You're right, that's what was messing it up. – jason adams Mar 25 '14 at 15:17
  • 1
    @chepner, that is incorrect. the else clause executes when the loop does not exit early. (i.e. the whole loop is exhausted) – acushner Mar 25 '14 at 15:20
  • Is there a list comprehension in order to this problem? – jason adams Mar 25 '14 at 15:20
  • @acushner I knew I should have double-checked that before commenting. – chepner Mar 25 '14 at 15:22

2 Answers2

6
while number > 2 and number < 10:
    for n in range(2, number):
        if number % n == 0:
            break
    else:
        primes_list.append(number)
    number += 1

>>> primes_list
[3, 5, 7]
>>> 

This is probably what you had in mind.

Note that number += 1 inside the first loop was misplaced

m.wasowski
  • 5,792
  • 1
  • 20
  • 30
Ant
  • 4,678
  • 2
  • 22
  • 42
  • The `number > 2` condition is not needed at all. – Joe Frambach Mar 25 '14 at 15:13
  • @JoeFrambach Of course. I just edited the number += 1 part ;-) – Ant Mar 25 '14 at 15:18
  • @Ant, so basically the break statement exits out of the for loop, but still executes number += 1? – jason adams Mar 25 '14 at 15:43
  • @jasonadams You enter for loop. If break is executed, than the program will run the next thing it finds, that is number += 1. If break is not executed, then it will run the else clause, but after that has finished, it will continue and will find the "number += 1 " command, which is therefore executed every time (as it should be) – Ant Mar 25 '14 at 15:47
1
for n in range(2, number):
    if number % n == 0:
        break
    number += 1

Because of the break, you never get to

number += 1 

when that condition is met. It will just keep appending number to the list forever.

Try something along the lines of:

primes_list = []
lower = 3
upper = 10
for number in range(lower, upper):
    if(isPrime(number)):
        primes_list.append(number)

print primes_list

#note this is not an efficient implementation
def isPrime(number):
    for n in range(2, number):
        if number % n == 0:
            return false
    return true
suavidas
  • 85
  • 3