This article has been dead for over three months: Start a new discussion instead

0

Hello,

I have new to programming in python(have a little experience in C++) and to this forum. I am trying to write a program that finds Nth prime number; for example the 1000th prime number. Whenever, I try to run the program I just a see a blinking cursor and the program stalls. Here is my code:

```
prime_status = 1
## Changes to indicate if number being tested is prime; 0 = false 1 = true
n = 998
## Nth prime number(used as counter for while loop); N = N -2 because dont account 0 and 3; therefore current status of Nth is 1000
number = 3
## Number being tested just being intialized; will be changed in loop
max_div = 0
## Maximum divisor just being intialized here, will later be given value
div = 2
## Current Divisor; Loop divided by this until div = max_div
while n > 0:
##Nth prime number;every time prime number is found the code will below will decrease n. When it hits 0; prime number that ##corresponds
## to n will have been found and this loop will BREAK
number + 1
## Everytime loop starts the number will increase by 1 to test all numbers.
max_dev = number / 2
## Found by number / 2 because for any WHOLE number x; if WHOLE number y = x / 2 ; x / (any number > y) will always have ## remainder indicating not prime.
##All these variables are to be given INTEGER status..because float(decimal) operation will return not prime...
##For example 11 / 2 = 5((WHOLE NUMBER)).... and 11 / (5 + 1)...11 /( 5 + ##2) ...etc etc will always have a remainder..
##So it is useless to test numbers bigger than half a number..because it is known that it will not have a remainder and will test not ##prime
div = 2
## Starting divisor...dont use 1 because obviously all numbers are
## divisible by 1
prime_status = 1
##Intially set as true
while div < max_div :
##This loop is broken if number being tested is found prime; or when all divisors have been tested and it is found prime
if number % div == 0 & div != number:
## If remainder is 0, then number is prime then enter code below; div != number is inserted for the following reason
## all numbers are divisible by themselves..so ignore this code if
## number being divided is equal to number.
div = max_div
##required to break loop
prime_status = 0
## Sets the prime status as false; to eliminate the later execution of code
elif number % div != 0:
##If the divisor gives a remainder of other than 0(so far prime); add 1 to test next divisor
div = div + 1
##Increase divisor and start loop again checking again for remainders.
##After the loop ends above number is found prime or not prime
if prime_status == 1:
n - 1
##Now if number is prime decrease n by 1; to account for master loop;
## Ex: If trying to find 1000th prime number is tested; Once first prime after 3..5 is detected..decrease n by 1
##and everything starts over...testing next number until next prime is detected..and on and on until Nth number is found
##Once everything is done Nth prime number has been found
## go back to master loop
print "1000th Prime #: " + number
```

I am still trying to learn good coding "grammar", hence the heavy commenting. So, if you can, please also let me know if its too much or what I can improve.

Any help is appreciated.

Thanks.

0

Generally, you use an increasing counter, increasing every time a prime number is found, until it reaches n. For how to find primes, there are literally thousands of posts on the web. http://www.daniweb.com/code/snippet216880.html

You