Guest User

Untitled

a guest
Feb 21st, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. print "I can compute prime numbers for you. Pick a positive integer 'n' and I will tell you what the nth prime number is."
  2. userselection = int(raw_input())
  3. collected_primes = [2]
  4. count = 0
  5. odd_integer =  1
  6. divisor = 2
  7. while userselection <= 0:
  8.     print str(userselection) + " is not a positive integer!"
  9.     break
  10. else:
  11.     while (count<userselection):
  12.         while divisor <= 0.5*odd_integer:
  13.             if odd_integer%divisor == 0:
  14.                 break
  15.             if odd_integer%divisor != 0:
  16.                 divisor = divisor+1
  17.         if divisor > 0.5*odd_integer:
  18.             count = count + 1
  19.             collected_primes = collected_primes + [odd_integer]
  20.         odd_integer = odd_integer+2
  21.         divisor = 2
  22.     if count >= 4:
  23.         print "The " + str(count) +"th prime is: " , collected_primes[-1]
  24.     elif count == 3:
  25.         print "The " + str(count) +"rd prime is: " , collected_primes[-1]
  26.     elif count == 2:
  27.         print "The " + str(count) + "nd prime is: " , collected_primes[-1]
  28.     else:
  29.         print "The " + str(count) + "st prime is: " , collected_primes[0]
Advertisement
Add Comment
Please, Sign In to add comment