Created
June 10, 2011 00:43
-
-
Save zrbecker/1018057 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| def isprime(p, plist): | |
| max = p ** 0.5 | |
| for i in range(0, len(plist)): | |
| if max < plist[i]: | |
| break | |
| if p % plist[i] == 0: | |
| return False | |
| return True | |
| def nprimeslist(n): | |
| plist = [] | |
| i = 2 | |
| while len(plist) < n: | |
| if isprime(i, plist): | |
| plist.append(i) | |
| i += 1 | |
| return plist | |
| def nthprime(n): | |
| plist = nprimeslist(n) | |
| return plist[n - 1] | |
| def main(): | |
| if (len(sys.argv) != 2): | |
| print "Usage: %s number" % sys.argv[0] | |
| sys.exit(1) | |
| n = int(sys.argv[1]) | |
| p = nthprime(n) | |
| print "The %dth prime is %d" % (n, p) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment