I've been tackling project euler, very slowly. I'm on problem 7, which can be seen here: https://projecteuler.net/problem=7
Essentially you're supposed to find the 10,001st prime number. I created a function that outputs a list of an input's factors:
def factor(number):
factors = []
for i in range(1, number + 1):
if i == number:
factors.append(i)
elif number % i == 0:
factors.append(i)
return factors
Then exploited the len() function to find primes:
def prime(number):
primes = []
for i in range(1, number + 1):
if len(factor(i)) == 2:
primes.append(i)
print("There are {} primes below {}.".format(len(primes), number))
Now I modified this to tackle the problem at hand.
def primes(num):
primes = []
a = 0
while len(primes) < num:
a = a + 1
if len(factor(a)) == 2:
primes.append(a)
print(len(primes))
print(primes[-1])
Now it worked, but it probably took 10+ minutes to run. I understand that this is because it's running through a ridiculous amount of numbers, I guess what I'm asking is what is a creative way to avoid creating a massive amount of criteria for the function to sort through? Not looking for a definitive answer, just trying to see actual coders' thought processes.