1

I'm trying to write some code to compute the thousandth prime number and when I thought I was done I tried to run it but nothing is happening. I don't get errors or anything that should happen according to the code, just literally nothing. I'm running it in windows powershell if that matters. the code is here below. (Python 2.7)

n = 1

all_odd_integers = 2*n+1
prime_number = 2 #cause 3 is the second prime

while prime_number < 1000: #a while loop to get to prime thousand
    for i in range  (2, (all_odd_integers-1)): #because I want to divide all_odd_integers by all numbers except 1 and itsself and nothing bigger, but bigger than 0
            if all_odd_integers % i == 0:
                n += 1
                print "not a prime" #because, like, if a number can be divided without a reminder it's not a prime
            else:
                n = n+1
                prime_number += 1
                print "a prime" #because exactly the opposite of the if-clause, here there is a reminder so prime

if prime_number == 1000:
    print "the thousandth prime is %d"  (all_odd_integers)
noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67
pokemonfan
  • 209
  • 1
  • 2
  • 9
  • #1 Tip for Debugging: Put random `print`s in your code to see what is happening. – noɥʇʎԀʎzɐɹƆ Feb 20 '15 at 22:25
  • Ex. Put a print in your `while` to see how many times it is executed. Then, since you know it's repeating, print `prime_number`. – noɥʇʎԀʎzɐɹƆ Feb 20 '15 at 22:27
  • @CrazyPython: although many debug with `print` statements, a better way is to use a [*debugger*](https://docs.python.org/2/library/pdb.html) since it allows one to inspect **all** variables at every point in the program. – Willem Van Onsem Feb 22 '15 at 20:18

5 Answers5

1

Your code isn't working because you have an infinite while loop, since your for loop isn't even being executed. You almost have the approach right for calculating the primes however.

If you want to print out every prime number as your current code would if it worked, you can much more easily implement something like this :

counter = 1
prime = 3

while counter != 1000:

    for x in range(2,prime):

        if prime%x == 0:       
            break
    else:
        print(prime)
        counter += 1

    prime += 2  

Alternatively, if you only want to print out the thousandth prime I'm sure there are a plethora of more efficient ways, but I would lean towards something like this for simplicity and decent efficiency:

from math import sqrt

counter = 1
prime = 1
while counter < 1000:

    prime += 2

    for x in range(2,int(sqrt(prime+1)) + 1):

        if prime%x == 0:       
            break
    else:
        counter += 1

print (prime)  
HavelTheGreat
  • 3,299
  • 2
  • 15
  • 34
0

The condition in your while loop is always true. prime_number doesn't ever change. Look again at your code

for i in range  (2, (all_odd_integers-1)):

What is this supposed to do if n=1 and therefore all_odd_integers-1 = 2? The code under this loop is never executed.

EDIT:

To loop over odd integers just do this:

for i in xrange(1,1000,2):
igavriil
  • 1,001
  • 2
  • 12
  • 18
  • how would I solve this then? I tried it without the -1 but ofcourse then the program will say everything is a prime (because like the remainder is zero so the else loop will be executed) strangely enough, the last if-statement will not be run and idk why? you'd think that after thousand times going through the while loop (cause the code stops after a while) the next statement would be executed – pokemonfan Feb 20 '15 at 21:48
  • `all_odd_integers` is a number. You should use 100 because that is one more than 999, the largest odd integer. – noɥʇʎԀʎzɐɹƆ Feb 20 '15 at 22:54
0

I think you're thinking about this too hard. There are some errors to your code, but really it can all be boiled down to:

counter = 1
n = 2

while True:
    n += 1
    if all(n % i for i in xrange(2, n)): counter += 1
    if counter == 1000: break

print "the thousandth prime is {0}".format(n)

This loops through all numbers between 2 and itself to see if any of them return a 0 remainder when n is divided by them. If none do, it's a prime and counter gets ticked, if not then it goes on to the next number. Once it finds 1000 primes it breaks and prints the thousandth prime is 7919

Some of the things wrong with your code:

  1. all_odd_integers never changes. So when you do for i in range(2, (all_odd_integers-1)) it will never run anything except for i in range(2, 2) which is 0 because range excludes the higher number. all_odd_integers = 2*n+1 is only run once, when all_odd_integers is defined, it doesn't change when n does.
  2. Even if all_odd_integers did update, why would you multiply it by two? Wikipedia has a prime defined as:

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

If n was 3 then all_odd_integers would be 7? Why would 3 need to be divided by 4-6?

  1. Just a note: there is no need to check if prime_number is equal to 1000 at the end since that's why the code block broke.
Daniel Timberlake
  • 1,179
  • 6
  • 15
0

Like the other anwserers, the condition in the while loop never changes. Look at your range() call:

range  (2, (all_odd_integers-1))

When you call range, there is a start (2) and a stop (all_odd_integers-1).

range(1,5) generates: 1,2,3,4

Look at all_odd_integers:

all_odd_integers = 2*n+1

2*n+1 means 2*1+1 because n = 1 so 2*n+1 = 3 therefore you are calling range(2,2) which gives [] and then the loop is never executed at all.

EDIT: More things

This function gives you your odd integers.

def odd_integers():
    odd_integers = []
    for i in xrange(1001):
        if i/2 != int(i/2);
           odd_integers.append(i)
    return odd_integers

Here's your new for:

for i in odd_integers():
noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67
0

What if you start with n=2 and prime_number = 3? that way you'll reach the for loop.

alex314159
  • 3,159
  • 2
  • 20
  • 28