# Python Challenge - Prime Number Finder

``````def prime_finder(n):

prime = []
num_list = []

for num in range(2, n + 1):
for x in range(1, num + 1):
if str(num / x)[-2:] == '.0':
num_list.append(x)
if len(num_list) == 2:
prime.append(num)
num_list = []

return prime

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

I was nervous to post my code to the forum because I assumed that my answer was going to be overly complicated compared to others but it looks very similar to most of them!

I realize now I could’ve just used modulus to find the whole numbers from the division but oh well, this way is a tad bit different than most.

Thanks for highlighting the discrepancies in the prime_finder function and sharing your valuable insights. I’ll definitely investigate and work on finding an improved solution.

def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True

def prime_finder(n):
prime_list =
for number in range(2, n+1):
if is_prime(number):
prime_list.append(number)
return prime_list

print(prime_finder(11))

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

This was my solution:

``````def prime_finder(n):
nums = [i for i in range(1, n+1)]
primes = []
for num in nums[1:]:
counter = 0
for i in nums:
if num % i == 0:
counter += 1
if counter == 2:
primes.append(num)
return primes
``````

I used a pretty simple solution. Considering I never want 1 or 0 in my finished list, I just started the counter at 2. I used two for loops giving me a time complexity of n^2 that can probably be streamlined a bit. My code runs each number separately and if it is ever evenly divisible by j (2 through that number minus one) it trips a flag and calls it true, which then doesn’t allow it to be added to the primes list.

def prime_finder(n): primes = []; for i in range(2, n+1): flag = False; for j in range(2, i): if i % j == 0: flag = True if flag == False: primes.append(i) return primes print(prime_finder(11))

This took me quite some time. My approach was as follows:

def prime_finder(n): answer=[i for i in range(2,n+1) if is_prime(i)] return answer def is_prime(x): for l in range(2,x): if x==l: continue elif x%l==0: return False else: continue return True print(prime_finder(11))

Following is a version of a solution for the prime finder challenge:

def find_primes(num): # Create the list of numbers to check primes = [] for n in range(2, num + 1): # Check the number in the prime list is_prime = True for i in range(2, int(n**0.5) + 1): if n % i == 0: is_prime = False break if is_prime: primes.append(n) return primes print(find_primes(11))

Are lines 6 and 7 even needed?

You are right! These lines are not needed

1 Like
def prime_finder(n): # Write your code here lst = [] # Empty list to hold the prime numbers for i in range(2, (n + 1)): # Loop through the number. It is inclusive say 11 # print(i, end=" ") # Output # 2 3 4 5 6 7 8 9 10 11 # Loop through 2 3 4 5 6 7 8 9 10 11 for j in range(2, i): # i is the input number for inner loop from the outer loop if i % j == 0: # Not prime number # Do nothing break else: # Prime number lst.append(i) return lst print(prime_finder(11))

Hello,

This is my version:

def prime_finder(n): prime_numbers = [] for i in range(2, n+1): if [j for j in range(2, i) if i%j == 0] == []: prime_numbers.append(i) return prime_numbers print(prime_finder(11))

hello guys, this is mine solution of this challenge:

def prime_finder(n): primes = list(range(2, n+1)) for x in range(1, n+1): for num in range(2, x): if x!=2 and x%num == 0: primes.remove(x) break return primes print(prime_finder(11))

def prime_finder(n):

prime_numbers =

# make a place to filter other numbers

garbage_numbers =

# get a list of numbers from n

list_of_numbers = range(n+1)

# start by iterating through numbers to find what is divisible by one and itself

for numbers in list_of_numbers:
if numbers == 0 or numbers == 1:
continue
if numbers > 2 and numbers%2 == 0 :
continue
#print(numbers)
if numbers > 3 and numbers%3 == 0 :
continue
if numbers > 5 and numbers%5 == 0:
continue
if numbers > 7 and numbers%7 == 0:
continue
if numbers > 11 and numbers%11 == 0:
continue
prime_numbers.append(numbers)
return prime_numbers

print(prime_finder(11))

Hi, This was my first challenge and I created this code.

def prime_finder(n):

prime_numbers =
for i in range(2, n + 1):
x = 2
prime_bool = True
while(prime_bool == True and x < i):
if i % x == 0:
prime_bool = False
x += 1
if prime_bool == True:
prime_numbers.append(x)
return prime_numbers

print(prime_finder(11))

Serious abuse of comprehension for a one liner.

def prime_finder(n): # Write your code here return [i for i in range(2, n+1) if all([i % j for j in range(2, i)])] print(prime_finder(11))
1 Like

My idea was:

1. make a list of all numbers up to n[1]
1. remove all numbers that are divisible by any number between (excluding) 1 and itself
``````def prime_finder(n):
listOfPrimes = list(range(2,n+1))
for potentialPrime in range(2,n+1):
for divider in range(2, potentialPrime):
if potentialPrime % divider == 0:
listOfPrimes.remove(potentialPrime)
break
return listOfPrimes
``````

1. unfortunately you can’t simply you the same list for the `for` loop. It skips some numbers, because IMHO the `.remove` is probably messing with the iterator. ↩︎

def prime_finder(n): # Write your code here new_list = [] for number in range(2, n+1): pieces = 0 for i in range(1, number+1): if number % i == 0: pieces +=1 if pieces == 2: new_list.append(number) return new_list print(prime_finder(1000))
1 Like

Here’s my solution (hard-coded empty lists for any number below 2, then a conditional comprehension within a comprehension to handle the prime-checking: if any int smaller than the current number but larger than 1 can divide evenly into it- i.e., modulo equals 0, which is false-y, then it is not prime):

``````def prime_finder(n):
if n < 2:
return []
else:
return [
x for x in range(2, n+1)
if all([x % i for i in range(x-1, 1, -1)])
]
``````
3 Likes

I’m not very good at recursion and this problem is always my goto to refresh myself how to implement it

``````def prime_finder(n):
primes = []
candidates = range(2, n+1)
count = n
for candidate in candidates:
if (prime(candidate)):
primes.append(candidate)
return primes

def prime(n, i=2):
if n == i:
return True
elif n % i == 0:
return False
return prime(n, i + 1)

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