Skip to content

Instantly share code, notes, and snippets.

@ekashida
Created July 4, 2013 00:24
Show Gist options
  • Select an option

  • Save ekashida/5923989 to your computer and use it in GitHub Desktop.

Select an option

Save ekashida/5923989 to your computer and use it in GitHub Desktop.
Find the 1000th prime number without defining any functions
# 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