Oct-05-2020, 07:24 PM
I tried your exact code
It gave me a wrong result
I changed that line that was confusing me to check if the current prime number is greater than the value's square root
And it worked perfectly
It gave me a wrong result
I changed that line that was confusing me to check if the current prime number is greater than the value's square root
And it worked perfectly
def prime(number): primes = [] primes.append(2) value = 3 while len(primes) < number: if check(primes, value): primes.append(value) value += 2 return primes[number+1] def check(primes, value): for prime in primes: if prime > math.sqrt(value): break if value % prime == 0: return False return True number = 10001 primes = prime(number) print('{}th prime = {}'.format(number,primes[number]))