1

So I am trying to find 6th prime number, and the while loop in getPrime is not working properly. It is supposed to end when count is bigger than num, but it doesn't. It'd be great if you could help me find out why.

import math

def isPrime(num):
    if num == 1:
        return False
    if num % 2 == 0 and num > 2:
        return False
    for i in range(3, int(math.sqrt(num))+1, 2):
        if num % i == 0:
            return False
    return True

def getPrime(num):
    count = 1
    while count < num:
        for i in range(1, 20):
            #print "check"
            if isPrime(i):
                print "prime", i
                count += 1
                print "count", count
            else:
                continue
    print i
    return i

getPrime(6)
9
  • So in short you want to know how many prime numbers are between 0 and num or get the highest prime number in some range? Why you are looking for prime numbers only in array [1, 20]? Commented Feb 10, 2015 at 8:14
  • 1
    Why are you using range(1, 20) exactly? Commented Feb 10, 2015 at 8:14
  • 2
    Also, i may not have been defined/initialized at all when you get to the end of the function. Commented Feb 10, 2015 at 8:17
  • What is problem statement?? getPrime(6) ?? want first 6 prime numbers or prime numbers from the first 6 numbers?? Commented Feb 10, 2015 at 8:20
  • He wants the 6th prime number starting at 1. So that is: 1, 3, 5, 7, 11, 13. Commented Feb 10, 2015 at 8:25

4 Answers 4

2

In the getPrime() function, when isPrime(i) returns False, you are not incrementing the count variable. So the while count < num loop gets stuck at that point.

Update: Well, that was my first impression from looking at the code. But then I noticed the nested loop. So I could have misread what was going on.

What I recommend at this point is stepping through the code in a debugger so you can see for yourself what is happening. Do you have a Python debugger available?

You can answer almost any question like this yourself if you have a good debugger and know how to use it. Then you won't have to wait for your friends on Stack Overflow to take guesses about what's going wrong! :-)

Sign up to request clarification or add additional context in comments.

Comments

2

The reason is because your range statement is within the body of the while statement. In another language you might use a do... until statement, but in Python the way to do it is just to add a conditional break statement, e.g. I have corrected your code to:

def getPrime(num):
    count = 1
    for i in range(1, 20):
        if count > num: break
        if isPrime(i):
            highestPrime = i
            count += 1
    return highestPrime

1 Comment

I have upvoted your answer, but it is still not correct completely. Add a highestPrime variable before the loop and assign the current value of i to it whenever i is a prime number. Then you can return highestPrime instead of i which may have no value.
0

You're only checking the value of count at the end of the for loop so you will always end up with the full range of 1-20 being tested. Why is the range limited to 20?

You don't need a for loop at all. Use the while loop to keep finding primes until you have num of them.

Try something like:

def getPrime(num):
    count = 0 
    i = 1 
    highPrime = None 
    while count < num:
        if isPrime(i):
            count += 1
            highPrime = i 
        i += 1
    return highPrime

Comments

0

Simplify getPrime, there is no need for a while loop:

def getPrime(num, r):
    gen = (i for i in r if isPrime(i))
    primes = zip(*zip(range(num), gen))[1]
    return primes[num-1] if len(primes) >= num else None

>>> print getPrime(6, xrange(1, 20))
13
>>> print getPrime(6, xrange(100, 500))
127

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.