0

I wrote this python code to find any prime number, the 1st, 2nd, 1000th, etc. I ran the code and it returned this, no matter what integer I entered:

2 is the 1 prime
3 is the 2 prime
5 is the 3 prime
7 is the 4 prime

Here is the code (written in python 2.7.8):

 #code to find the nth prime
def isprime(n):
    '''check if integer n is a prime'''
    # make sure n is a positive integer
    n = abs(int(n))
    # 0 and 1 are not primes
    if n < 2:
        return False
        # 2 is the only even prime number
    if n == 2:
        return True
        # all other even numbers are not primes
    if not n:
        return False
        # range starts with 3 and only needs to go up the squareroot of n for all odd numbers
    for x in range(3, int(n**0.5)+1, 2):
        if n % x == 0:
            return False
    return True
num_ofprimes = 0
candidate_prime = 2
final_primes = raw_input("What prime would you like to find?")
while num_ofprimes <= final_primes:
    if isprime(candidate_prime) == True:
        if candidate_prime == 2:
            num_ofprimes = num_ofprimes + 1
            print (str(candidate_prime) + " is the " + str(num_ofprimes) + " prime")
            candidate_prime = candidate_prime + 1
            #2 is prime
        elif candidate_prime % 2 == 0:
            candidate_prime = candidate_prime + 1
            #if a number is even it is not prime
        else:
            num_ofprimes = num_ofprimes + 1
            print (str(candidate_prime) + " is the " + str(num_ofprimes) + " prime")
            candidate_prime = candidate_prime + 1
            # checks all odd numbers to see if prime then prints out if true
print ("All done!")
Roark Piacente
  • 71
  • 1
  • 1
  • 4

3 Answers3

1

Your program does not stop after it found those first few primes, but it runs into an infinite loop, generating no more output. The reason for this is that if your isprime check fails, you never increment the candidate_prime variable!

Also, as noted in comments, you should compare num_ofprimes to int(final_primes); otherwise you are comparing an int to a str, which is much like comparing apples to oranges.

Finally, you should put the check whether the number is even inside your isprime function. Not only will this make your isprime function actually return correct results for even numbers, but it will also make your code a whole deal more compact, as you no longer need all those if/elif/else blocks below your if isprime check.

tobias_k
  • 74,298
  • 11
  • 102
  • 155
0

In addition to tobias's important comments, you seem to be doing a lot of extra work in your script for no reason. The while loop can be simplified to:

while num_ofprimes < final_primes:
    if isprime(candidate_prime):
            num_ofprimes = num_ofprimes + 1
            print (str(candidate_prime) + " is the " + str(num_ofprimes) + " prime")
    candidate_prime = candidate_prime + 1
eigenchris
  • 5,331
  • 1
  • 17
  • 29
  • 1
    But only if the even-number-check is moved to `isprime` instead. – tobias_k Feb 11 '15 at 22:23
  • @tobias_k Yes, good point. I changed the `not n` condition in the function to `n%2!=0` in my version of the script and forgot to mention that. – eigenchris Feb 11 '15 at 22:24
0
for num in range(3, 1000):
    if num > 1:
        for i in range(2, num):
            if (num % i) == 0:
                break
        else:
            print(num)
Haresh Shyara
  • 1,442
  • 8
  • 13
  • Usually it is helpful to include some text which explains how the code answers the question or solves the problem in addition to bare code. – Makyen Feb 12 '15 at 10:46