How can I continue the for loop of the outer side? - python

def countPrimes(self, n):
if n <= 1:
return 0
if n == 2:
return 1
count = 0
counted = [2, ]
for num in xrange(3, n+1, 2):
for c in counted:
if num % c == 0:
continue
count += 1
counted.append(num)
return count
I am writing a code for the solution of primes counting problems. I used counted as an array storing for primes that have been examined and use them for the examination for the next prime. I tried using continue to drop out the inner for loop then count += 1 and counted.append(num) would not be executed once the num is found not a valid prime. However, I met implementing problem here, as the continue statement would take me to another c instead of another num.

If I understand your question correctly, you want to know how to break the inner c loop, avoid the other code, and continue with another num. Like the other answers, you want to make use of break with some smart booleans. Here's what the loop might look like:
for num in xrange(3, n+1, 2):
next_num = False
for c in counted:
if num % c == 0:
next_num = True
break
if next_num:
continue
count += 1
counted.append(num)
This way, if you encounter a num that is divisible by c, you break out of the inner c loop, avoid adding num to the counted list.

It doesn't make a ton of sense to accumulate a count and a list of primes as you go. Introduces the chance for a mismatch somewhere. I've massaged the code a bit, with some more descriptive variable names. It's amazing how clear names can really make the algorithm make more sense. (At least for me it does)
def primesUpTo(n):
primes = []
if n > 1:
primes.append(2)
for candidate in xrange(3, n+1, 2):
for prime in primes:
candidate_is_prime = candidate % prime
if not candidate_is_prime: # We're done with this inner loop
break
if candidate_is_prime:
primes.append(candidate)
return primes
print len(primesUpTo(100))

Try using break instead of continue. See the documentation here. Basically break takes you out of the smallest enclosing loop, spitting you back into your higher-level loop for its next iteration. On the other hand, continue just jumps you on to the next iteration of the smallest closing loop, so you wouldn't go out to the higher-level loop first.

Related

While loop doesn't read condition

I'm trying to make a program that will stop when it gets 5 prime numbers from a range.
I've completed most of the program except the part where it is supposed to stop after it gets 5 numbers.
I've added a condition for it to stop, once the counter reaches 5 but it does not stop and continues to list all the numbers in the range.
Here is code I have:
condition = 0
while condition < 5:
for numbers in range(2,20):
for divisor in range(2,numbers):
if (numbers % divisor) == 0:
break
else:
print(numbers)
condition +=1
The condition+=1 never goes through and it lists all the prime numbers from 1 to 20 even though I just want the first 5.
I've tried spacing options with the "condition +=1" but it still does not work
Any help would be appreciated
While is out of for loop, so cannot work obviously. A simple solution is to check required condition later:
for numbers in range(2,20):
for divisor in range(2,numbers):
if (numbers % divisor) == 0:
break
else:
print(numbers)
condition +=1
if condition >=5:
break
I think the real problem you are having is that you have written bad code. A much better approach to this problem is to isolate as many pieces as possible.
for example:
def is_prime(x):
"return true if x is prime, otherwise false"
# implement me!
return True
def get_first_n_primes_less_than_y(n, y):
number = 2
condition = 0
while condition != n and number < y:
if is_prime(number):
print(number)
condition += 1
number += 1
get_first_n_primes(5, 20)
The above code, with some tweaking can perform the same task. However, code like this is much simpler to debug and reason about because we have isolated chunks of code (is_prime, has nothing to do with the while loop)
num_results = 5
counter = 0
range_start = 2
range_end = 20
# iterate range
for number in range (range_start, range_end):
# iterate divisors
for divisor in range (2, number):
# check division
if (number % divisor) == 0:
break
else:
print ("%s is a prime number" % number)
counter += 1
break
# check if number of results has been reached
if counter == num_results:
break
# check if number of results has been reached
if counter == num_results:
break
The problem is that you need to run the entire content of the while block before you test the condition again.
Here is a way around
condition = 0
numbers=2
while condition < 5 and numbers < 20:
for divisor in range(2,numbers):
if (numbers % divisor) == 0:
break
else:
print(numbers)
condition +=1
numbers+=1

Python Code to Find x Prime Numbers w/ For Loop?

I am trying to put together a simple program which could work out n prime numbers. I would like to do this by using a nested for loop, where one would go through the numbers, and another would divide that number by all of the numbers up to it to see if it would be divisible by anything.
The problem I am having is that in the main for loop, I need to start it at 2, seeing as 1 would mess up the system and I don't want it to be considered a prime. For the loop to have a starting number however, it also needs an ending number which is difficult in this instance as it is hard to generate the largest prime that will be needed prior to the loop working.
Here's the program that I am using right now. Where I have marked X is where I need to somehow put an ending number for the For Loop. I guess it would be much simpler if I let the For Loop be completely open, and simply take out anything that '1' would produce in the loop itself, but this feels like cheating and I want to do it right.
check = 0
limit = int(input("Enter the amount of Prime Numbers"))
for i in range(2,X):
check = 0
if i > 1:
for j in range(2,i):
if (i % j) == 0:
check = 1
if check == 0:
print (i)
Thanks for your help!
You can step through an unlimited amount of numbers using a generator object.
Insert the following somewhere near the top of your code:
def infinite_number_generator(initial_value=2):
""" Generates an infinite amount of numbers """
i = initial_value
while True:
yield i
i += 1
What this does is it creates a function for constructing generator objects that "pause" whenever they reach the yield statement to "yield" whatever value is specified by the yield command, and then continue to execute from the next line beneath the yield statement.
Python's own range function is itself an example of a generator, and is roughly equivalent to (ignoring the step argument and other peculiarities)
def range(start, end):
i = start
while i < end:
yield i
i += 1
So your program would then look like this:
def infinite_number_generator(initial_value=2):
""" Generates an infinite amount of numbers """
i = initial_value
while True:
yield i
i += 1
check = 0
limit = int(input("Enter the amount of Prime Numbers"))
for i in infinite_number_generator():
check = 0
for j in range(2,i):
if (i % j) == 0:
check = 1
if check == 0:
print (i)
if i == limit:
break
I should also point out that the code you provided is buggy - it will never stop printing because there's no checking whether you've found your limit number of primes yet or not.
This should do what you want.
check = 0
limit = int(input("Enter the amount of Prime Numbers"))
counter = 0
i = 2
while counter < limit:
check = 0
if i > 1:
for j in range(2,i):
if (i % j) == 0:
check = 1
if check == 0:
counter += 1
print (i)
i += 1
In your code you start i with 2 and always increment by 1, so the i will always remain greater than 1, therefore the test if i > 1 is useless.
For efficiency you can stop the check at the square of i or i/2 (no divisors in [i/2 + 1, i[ ).
you can update your code as follow:
n = int(input("Enter the amount of Prime Numbers: "))
FoundPrimes = 0
i = 2
while FoundPrimes < n:
isPrime = True
for j in range(2,1 + i//2):
if (i % j) == 0:
isPrime = False
if isPrime:
FoundPrimes += 1
print(i, end = '\t')
i += 1

Python prime listing repetition bug

Trying to create a program that lists the prime numbers in a set interval (has to be between 1 and 500), so user input isnt allowed. This is my code so far:
list=[]
for num in range(1, 500):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
list.append(str(num))
print(','.join(list))
However, when I run this code some of the primes are repeated multiple times in the list, making it much longer than it should be. How can i fix this? Thanks in advance for any help.
You only want to add the number to the list of primes after you've checked all the possible divisors. That means you only add it after the for loop has completed.
A good way to do this is with the optional else clause on for loops.
Loop statements may have an else clause; it is executed when the loop
terminates through exhaustion of the list (with for) or when the
condition becomes false (with while), but not when the loop is
terminated by a break statement.
You can improve this function by only checking the modulus of primes you've already found. Since all non-prime numbers have prime roots, you only need to check if a number is divisible by lesser primes to determine if it is a prime.
primes = []
for i in range(2, 501):
for p in primes:
if i % p == 0:
break
else:
primes.append(i)
In the loop:
for i in range(2, num):
if (num % i) == 0:
break
else:
list.append(str(num))
you are appending the prime once for every i. The else should line up with the for:
for i in range(2, num):
if (num % i) == 0:
break
else: # note: different indentation!
list.append(str(num))
In python loops can have an else clause which is executed only if no break was executed inside the loop. They can be used to avoid boolean flags in a lot of cases.
So the code:
found = False
for x in iterator:
if predicate(x):
found = True
break
if not found:
# do default action
can be replaced by:
for x in iterator:
if predicate(x):
break
else:
#do default action
Make sure you are adding each only once.
The problem is that until you find a number i that divides num, num is added to the list. It is straightforward that you will have each prime in the list many times. In fact if n is prime, you will have n in the list n-2 times. You will also have all odd numbers in the list.
Only even numbers won't appear in the list, because the first number against which you are testing the divisibility is 2 and 2 divides all even numbers.
Without changing too much in your algorithm, you can introduce a flag isPrime that is set to True by default, and whenever you find i that divides num you set isPrime to True before breaking the loop. Your program becomes:
list=[]
for num in range(1, 500):
if num > 1:
isPrime=True
for i in range(2, num):
if (num % i) == 0:
isPrime = False
break
if isPrime:
list.append(str(num))
print(','.join(list))

Better algorithm on prime numbers

I'm working on a program which will found the nth. prime number. For example, By listing the first six prime numbers: 2, 3, 5, 7, 11 and 13, we can see that the 6th prime is 13. I'm trying to making an algorithm, like, if I want to see 50th prime, I will add 1 to ends of the range() function. I'm using this algorithm to find primes at the moment;
cnt = 1
print (2)
for x in range(3,40,2):
div = False
for y in range(2,round(x**0.5)+1):
if x%y == 0:
div = True
if div == False:
print (x)
cnt += 1
print ("\nThere is {} prime numbers.".format(cnt))
You see that, I put 40. But I want to put n there, so for example, untill reaching the 50th prime, add +1 to n. But it's not going to like that, if I tried something like;
cnt = 1
n = 40 #example
while cnt<50:
for x in range(3,n,2):
#codes
if div == False:
n += 1
I thought when the program finds a prime, it will add +1 to n and while loop will process untill find the 50th prime. But it didn't, primes are wrong if I use this one also, nothing relevant what I want to do.
How to make this algorithm, obviously changing the last element of range() function does not working.
Is there better/elegant algorithm/way? If I want to find 200.000th prime, I need faster codes.
Edit: I was working with lists first but, I got MemoryError all the time when working with big numbers. So I pass that and using a variable that counting how much primes are there cnt.
Here is a much faster version
primes = []
primecount = 0
candidate = 2
while primecount<50:
is_prime = True
for prime in primes:
if candidate%prime == 0:
is_prime = False
break
elif candidate < prime**2:
break
if is_prime:
primes.append(candidate)
primecount += 1
candidate += 1
print primes[-1]
note small edit adding the candidate<prime**2 test that OP included but I neglected initially.
Your code is going to be very slow for several reasons. If 2 divides a number you know it's not prime, but you're still checking whether 3 or 4 or 5... divides it. So you can break out as soon as you know it's not prime. Another major issue is that if 2 does not divide a number, there's no reason to check if 4 divides it as well. So you can restrict your attention to just checking if the primes coming before it divide it.
In terms of run time:
First off, for backwards compatibility with python 2, I added an int() around your rounding of root x.
From what I understand of your question, you are looking for something like this:
cnt = 1
maximum=50 #Specify the number you don't want the primes to exceed
print (2)
n=20 #highest number
while cnt<maximum: #
for x in range(3,n,2): #using "n" as you hoped
div = False
for y in range(2,int(round(x**0.5)+1)):
if x%y == 0:
div = True
if div == False:
if x<maximum: #we only want to print those up to the maximum
print str(x)
else: #if it is greater than the maximum, break the for loop
break
cnt += 1
break #break the while loop too.
print ("\nThere are {} prime numbers.".format(cnt))
This gave me the correct primes. As for better/more elegant solutions. If by better you want speed, use a library that has an optimized, or look at this for an example of a fast program. Elegance is subjective, so I will leave that to the rest of the internet.

Division by values in a dictionary (Python)

I am building a prime generator (I know, another one and probably not a very good one at that, but that is for another question.) I am using a dictionary for my collection of primes and ignoring composites by dividing by the previous primes. However it doesn't seem to be iterating properly in the final stage of the function and I get a number of incorrect results. isWhole is a self explanatory call to another function. This is my code where x = the number of primes to be generated:
def prime_generator(x):
count = 2
counter = 2
p = {1: 2}
while len(p) <= x:
if count % 2 == 0:
count += 1
continue
test1 = (math.sqrt(count))
if isWhole(test1) == True:
count += 1
continue
for k, a in p.items():
if count % a == 0:
break
else:
p[ counter ] = count
counter += 1
break
count += 1
return p
Your design intent is not entirely clear, but you may be intending to have the else clause apply to the for loop rather than the if statement. Try un-indenting the entire else clause so that it only runs if the loop terminates without hitting a break.

Categories