Reputation: 11
I am working on a program that finds prime numbers in order to learn about for loops. I have a list of some primes that can be used to check % == 0. I've tried this
primes = [1, 3, 5, 7, 9, 11, 13, 17, 19, 29]
hold_the_primes = []
for x in range(29,841):
for y in primes:
if x % y == 0:
pass
else:
hold_the_primes.append(x)
primes.extend(hold_the_primes)
for x in primes:
print x
but it returns nothing and the terminal gets stuck at this point. What can I do to accomplish this?
Upvotes: 0
Views: 122
Reputation: 56
Nested loops are possible in python. The problem is with your code I believe.
Here are some pointers:
hold_the_primes.append(x)
will be called for when y = 3, 5, 7, 9, 11, 13, 17, and 19.Here's an example that would work I believe:
primes = [2, 3, 5, 7, 9, 11, 13, 17, 19, 23, 29]
hold_the_primes = []
def isPrime(n):
temp = 2;
while temp*temp <= n: #temp < math.sqrt(n):
if n % temp == 0:
return False
temp += 1
return True
for x in range(30,841):
if isPrime(x):
hold_the_primes.append(x)
primes.extend(hold_the_primes);
for x in primes:
print x
Upvotes: 1