I have just begun to learn python, so my question is probably silly. Just for practice I want to write a function which calculates the first n primes. prime computes some prime numbers and keeps them in a list. Then tries another number and if it's not even divisible by the already discovered numbers then adds it in the list. But unfortunately, as you may already have guessed, it doesn't work.
from math import sqrt
from math import floor
def prime(n):
primes = []
# how many prime number have been discovered
i = 3
# the list of them
primes.append(1)
primes.append(2)
primes.append(3)
# the last prime number
a = 3
while i < n:
# the next prime candidate
a = a + 2
for j in range(i):
# if it isn't prime, search for another
if floor(sqrt(a)) < primes[j] or a % primes[j] == 0:
break
### this condition is never true. Why?
if j >= i - 1 or floor(sqrt(a)) < primes[j]:
primes.append(a)
i += 1
print i
return primes
result = prime(10)
print result[-1]