# Python Challenge - Prime Number Finder

def prime_finder(n):

# Write your code here

prime=

for i in range(2,n+1):
flag=0
for j in range(2,i):
if (i%j == 0):
flag =1
break
if (flag==0):

``````  prime.append(i)
``````

return prime
print(prime_finder(11))

``````def prime_finder(n):
prime_list = [True] * (n + 1)
prime_list[:2] = [False] * 2
primes = []
for index in range(len(prime_list)):
if prime_list[index]:
primes.append(index)
for idx in range(index*index, n + 1, index):
prime_list[idx] = False
return primes
``````
def prime_finder(n): result = [] for i in range(2,n+1): for j in range(2, int(i/2)+1): if i % j == 0: break else: result.append(i) return result print(prime_finder(20))
def prime_finder(n): # Write your code here primes = range(2, n+1) evens = [] for i in primes: for j in range(2, i): if i % j == 0: evens.append(i) return list(filter(lambda x: x not in evens, primes)) print(prime_finder(11))
def prime_finder(n): # Write your code here# factors = [] for number in range(n + 1): if number > 1: for factor in range(2, number): if (number % factor) == 0: break else: factors.append(number) return factors print(prime_finder(11))
``````def prime_finder(n):
primes = []
current = 2
while current <= n:
if not any([(current % prime == 0) for prime in primes]):
primes.append(current)
current+=1
return primes
``````

def prime_finder(n):
prime_numbers =
for i in range(2,n+1):
count = 0
for j in range(2,i+1):
divide = i/j
if divide%1 == 0:
count+=1
if count == 1:
prime_numbers.append(i)
return prime_numbers

print(prime_finder(11))

def prime_finder(n): # Write your code here prime_list = [ ] for i in range(1,n+1): test_list = [ ] for j in range(2,n+1): if i % j == 0: test_list.append(i) if len(test_list) == 1: prime_list += test_list return prime_list print(prime_finder(11))

# Using the Sieve of Erathostenes

from math import sqrt

def prime_finder(n):
if n <= 1:
return
a = [True for _ in range(2, n+1)]
for i in range(2, int(sqrt(n)+1)):
if a[i-2]:
j = i*i
while j <= n:
a[j-2] = False
j += i
return [i for i in range(2, n+1) if a[i-2]]

print(prime_finder(11))

Here you go. Solves the problem, could also be improved with some list comprehension

``````def prime_finder(n):
# Work out which numbers are primes.
prime_list=[]
for i in range(2,n+1):
if number_is_prime(i):
prime_list.append(i)
else:
continue
return prime_list

def number_is_prime(n):
# Function to check if a number is a prime

number_to_test_list = [n]*(n-2) # neglect 1 and n
numbers_list = list(range(2,n)) # Neglect 1 and n

list_mod = []
for x,y in zip(number_to_test_list,numbers_list):
list_mod.append(x%y)

# if all of the items are non-zero, then it is a prime number
if(all(item!=0 for item in list_mod)):
return True
else:
return False

print(prime_finder(11))
``````

def prime_finder(n):

# Write your code here

list_n = [2]
prime = False
for i in range(2,n+1):
for j in range(2,i):
if (i%j)==0:
prime=False
break
else:
prime=True
if prime:
list_n.append(i)
prime = False
return list_n

print(prime_finder(11))

import math
print(math.floor(math.sqrt(9))+1)
def check_prime(n):
if n <= 1:
return False
elif n > 1 and n <= 3:
return True
else:
for i in range(2,math.floor(math.sqrt(n))+1):
if n % i == 0:
return False
return True

def prime_finder(n):
if n <= 1:
return 0
lst =
for i in range(2,n+1):
if check_prime(i) == True:
lst.append(i)
return lst

print(prime_finder(11))

Can anybody help and let me know what the tests are? My code works fine but it only passes 4/5 tests

def prime_finder(n): # Write your code here primes_to_n = [] for i in range(2,n): if isinstance(is_prime(i), int): primes_to_n.append(i) return primes_to_n def is_prime(x): count = 0 if x <= 1 or not isinstance(x, int): return 'cannot compute' for i in range(1, int(x/2) + 1): if x%i != 0 or i == 1: count += 1 # print((count, i)) if count == int(x/2): return x elif x%i == 0: return 'not prime' print(prime_finder(11))

The instructions specify:

For example, `prime_finder(11)` should return `[2, 3, 5, 7, 11]`

Your code returns `[2, 3, 5, 7]`

2 Likes

Had some issues because I had the else indented to the wrong location, but I eventually figured it out!
def prime_finder(n):

primes = [] for i in range(2, n + 1): for j in range(2, int(i**0.5) + 1): if i%j == 0: break else: primes.append(i) return primes print(prime_finder(11))
1 Like

Take a look at your range, since you want to include 11 in your tested values.

1 Like
``````def prime_finder(n):
p_list = []

if n > 1:
n_list = list(range(2,n+1))
while len(n_list) != 0:
prime = n_list[0]
p_list.append(prime)
m = 1
while m*prime <= n:
if m*prime in n_list:
n_list.remove(m*prime)
m += 1
return p_list
``````
``````
def prime_finder(n):

list_of_primes = []
isPrime = None

#If n is less than 2 don't check for primes
if n < 2:
return list_of_primes

#If n is greater than 2, add 2 to list_of_primes and check for primes starting from 3
if n >= 2:
list_of_primes.append(2)

for test in range (2,n + 1):
for i in range (2, test):
if test % i == 0:
isPrime = False
break
else:
isPrime = True

if isPrime == True:
list_of_primes.append(test)

return list_of_primes

print(prime_finder(11))

``````

I am 25% complete in the Data Scientist: Analytics Specialist path. Here is my solution to the Prime Numbers function.

``````def prime_finder(n):
numRange = list(range(n+1))
primeArray = []

for num in numRange:
if num > 0:
primeTracking = 0
for tNum in list(range(1, num+1)):
if num % tNum == 0:
primeTracking += 1
if primeTracking == 2:
primeArray.append(num)
return primeArray
``````

Can anyone tell me why my indent is not showing in my code? This is my first post on here.
edit: Figured it out. In case anyone has the same problem, highlight all code and press the </> button.

def prime_checker(n):
if n==1:
return False
elif (n>2 and n%2==0) or (n>3 and n%3==0):
return False
for i in range(5,int(n**0.5)+1,6):
if n%i==0 or n%(i+2)==0:
return False
return True

def prime_finder(N):
primes=
for i in range(2,N+1):
if prime_checker(i)==True:
primes.append(i)
return primes

print(prime_finder(11))