Hope you can give me some insights in how I can improve my code:)
def prime_finder(n):
#We create a list where will save the prime numbers that we find
primes =
#And a list for all not prime numbers
not_primes =
#We do this by searching all the numebrs from 2 to n
for index in range(2,n+1):
#If the number is not in the not_primes list the we add it to the prime numbers list
if index not in not_primes:
primes.append(index)
#After we added the prime number to our list we look for all the multiples of our prime number, starting by the square of it until we reach the half of n (i thin this can be done until the square root of n or something similar to that but to not make any mistakes doing it untill the half works)
for index_2 in range(index,int(n/2)+1):
not_primes.append(index*index_2)
else:
#If the index is in the not_primes list that means that it is not a prime number, so we can skip with the next number of the range
continue;
#Finally the function returns the list of prime numbers
return primes
print(prime_finder(11))
def prime_finder(n):
import math #importing library to be able to use math.sqrt() function
def prime_finder(n):
primes= # creating empty list for storing primes
for i in range(2, n+1): # starting checking from number 2, because 0 and 1 are not primes
if i==2 or i==3: # 2 and 3 are primes
primes.append(i)
continue
elif i%2==0 or i%3==0: #I’m skipping numbers divided by 2 and 3
continue
else:
is_prime = True
for iter in range(5, int(math.sqrt(i)+1), 6):
if i%iter==0 or i%(iter+2)==0:
is_prime = False
break
if is_prime == True:
primes.append(i)
return primes
print(prime_finder(17))