Created
July 4, 2013 00:24
-
-
Save ekashida/5923989 to your computer and use it in GitHub Desktop.
Find the 1000th prime number without defining any functions
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
| # Check for primes starting at 2 since 1 is not prime by definition | |
| number = 2 | |
| # Store all primes in this list | |
| primes = [] | |
| # Keep looking for primes until we find 1000 of them | |
| while len(primes) < 1000: | |
| # Assume the number is prime until proven otherwise | |
| is_prime = True | |
| for prime in primes: | |
| # If there's no remainder, then the number was cleanly divided by a | |
| # prime and is not a prime itself | |
| if number % prime == 0: | |
| # Flag it | |
| is_prime = False | |
| # Get out of this for-loop | |
| break | |
| if is_prime: | |
| primes.append(number); | |
| # Now check the next number | |
| number = number + 1 | |
| # Print the 1000th prime | |
| print primes[-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment