# Python Challenge - Prime Number Finder

def prime_finder(n):
prime =
for i in range(2,n+1):
if len(prime) == 0:
prime.append(i)
else:
count = 0
for j in prime:
if i%j != 0:
count+=1
else:
count-=1
if count == len(prime):
prime.append(i)
return prime

print(prime_finder(11))

``````def prime_finder(n):
res = []
IsPrime = [True] * (n + 1)
IsPrime = False
IsPrime = False
d = 2
while d * d <= n:
if IsPrime[d]:
for i in range(d * d, n + 1, d):
IsPrime[i] = False
d += 1
for i in range(n + 1):
if IsPrime[i]:
res.append(i)
return res
print(prime_finder(13))
``````
def prime_finder(n): # Write your code here primes = [] for num in range(2, n+1): for i in range(2, num//2 +1): if num % i == 0: break else: primes.append(num) return primes print(prime_finder(11))

for this solution you have to make use of the uncommon for/else statement. if you just do a regular if/else statement within the inner loop you will get duplicate prime numbers in your list

def prime_finder(n): # Write your code here def isprimeultimate(n: int): for x in range(2, int(n**0.5) + 1): if n % x == 0: return False return True return [int(x) for x in range(2,n+1) if isprimeultimate(x) == True] print(prime_finder(11))

Here a nice oneliner solution for those of you who are a bit more advanced:

``````prime_finder = lambda n: [i for i in range(2, n + 1) if 0 not in [i % j for j in range(2, i)]]
``````
def prime_finder(n): prime = [] for numerator in range(2, n+1): divisible = 0 for denominator in range(2, n+1): if numerator == denominator: continue #skip the iteration if the number is itself if numerator % denominator != 0: continue # skip the iteration if not divisible divisible += 1 break # if the number is divisible in this iteration, we don't need to check the rest of denominator. This is not a prime number. if divisible == 0: prime.append(numerator) #because we skip 1 and the number itself, divisible should be 0 for prime number return prime print(prime_finder(11))
``````def prime_finder(n:int):
return [i for i in range(2, n+1) if all([i % j != 0 for j in range(2, i)])]
``````

def prime_finder(n):

list =
for number in range(1,n+1):
if number>1:
for prime in range(2,number):
if number%prime == 0:
break
else:
list.append(number)
return list

print(prime_finder(11)) Hi! So my solution is as follows:

``````def prime_finder(n):
# Initiate empty list
primes = []
# Looping through numbers smaller than n
for i in range(2,n+1):
isprime = True
# Checking if i is a prime
for k in range(2,i):
if i % k == 0:
isprime = False
break
# if it's a prime, add to the list
if isprime:
primes.append(i)
return primes``````
import math def isPrime(n): if n <= 3: return n > 1 if (n % 6 != 1) and (n % 6 != 5): return False sqrt = math.sqrt(n) i = 5 while(i <= sqrt): if (n % i == 0) or (n % (i + 2) == 0): return False i += 6 return True def prime_finder(n): # Write your code here prime_numbers = [] for i in range(1, n+1): if isPrime(i): prime_numbers.append(i) return prime_numbers print(prime_finder(1000000))
def is_prime(p): for i in range(2, p): if p%i == 0: return False return True def prime_finder(n): prime_list = [] for i in range(2, n+1): if is_prime(i): prime_list.append(i) return prime_list print(prime_finder(11))
def prime_finder(n): prev_nums = [i for i in range(2, (n+1))] prime_numbers = [] for num in prev_nums: remainders = [] for i in range(2, 11): if i != num: remainders.append(num%i) if 0 not in remainders: prime_numbers.append(num) return prime_numbers print(prime_finder(110))