Project Euler problem 7 says:
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number?
Here's the code I've written in Python:
def primes(n):
primes = []
attempt = 3
while len(primes) < (n-1):
for i in range(len(primes)):
if attempt % primes[i] == 0:
attempt += 2
break
else:
primes.append(attempt)
print (primes)
return (primes)
While testing a number, if it finds that the number is divisible by one of the primes in the list, the for loop breaks, and starts again with a larger number. If it isn't divisible, it's added to the list of primes. This keeps going until the list is as large as desired (in this case 10000, and I've omitted 2 so the last member of the list is the 10001st prime)
The problem is that this is extremely SLOW. I've seen other solutions that can apparently solve this is seconds, if not less. What are some ways I could make this fun faster?