Python Challenge - Prime Number Finder

try: def prime_finder(n): x = 1 primes = [] while x < n + 1: i = 1 if i == x: primes.append(x) x += 1 else: divisores = [] while i <= x: divisors = [] if x % i == 0: divisors.append(i) divisores.append(i) if len(divisors) > 2: x += 1 else: i += 1 else: i += 1 if len(divisores) <= 2: primes.append(x) x += 1 else: x += 1 primes.pop(0) return primes print(prime_finder(200)) except: print("S O R R Y")

Hello everyone! I´ve just started covering some python essentials, though I could not wait to tackle some of the coding challenges. There are surely better and more straightforward ways of tacking the challenge, but who knows… Maybe someone chose the same path as me and so this could be helpful.

I made my solution only checking with prime’s numbers (not all other numbers) & only check odd numbers, this must be faster than other aproachs.

def prime_finder(n): # Write your code here primes = [2] if n >= 2 else [] for num in range(3, n+1, 2): div_count = 0 for p in primes[1:]: if num % p == 0: div_count += 1 if div_count > 1: break # more than 2 divs (not prime) if num // p < p: break # All extra primes are useless if div_count == 0: primes.append(num) return primes print(prime_finder(11))

My solution, using the Eratosthenes algorithm.


type or paste code here

def prime_finder(n):
  primes = [True] * (n + 1)
  primes[0] = primes[1] = False
  for num in range(2, int(n**0.5) + 1):
      if primes[num] == True:
          for multiple in range(num * num, n + 1, num):
              primes[multiple] = False
  prime_numbers = [num for num, is_prime in enumerate(primes) if is_prime == True]
  return prime_numbers


The original poster’s basically saying that the default is that the number isn’t a prime. Hence, p = False. Now, if the original number is divided by some number and the remainder after division is zero, it automatically means the original number is not a prime: so p = True. In Python:

if num % y == 0:
        p = True

I believe that the OP could make p = True as default. and then ask:

if p:

def prime_finder(n):

Write your code here

prime_numbers =
for num in range(n+1):
if num > 1:
for i in range(2, num):
if (num % i) ==0:
return prime_numbers


def prime_finder(n): # Write your code here prime = [] for j in range(2,n+1): count =0 for i in range(2,j): if j%i ==0: count =+1 if count == 0: prime.append(j) return prime print(prime_finder(11))
def prime_finder(n):
  while i<=n:
    for j in range(2,i):
      if i%j==0:
    if len(no_primes)==0:
  return primes
def prime_finder(n): # Write your code here return [num for num in range(2, n+1) if (lambda num: all(num%i != 0 for i in range(2,int(num**0.5+1))))(num)] print(prime_finder(11))

I used a lambda function to solve this challenge. The reason for using num**0.5 (square root of the number) is for optimization. If a number doesn’t have a divisor smaller than its square root, then it won’t have any divisors larger than its square root either.

1 Like
def prime_finder(n): # Write your code here test = [] count = [] prime_list = [] for i in range(2, n+1): test.append(i) for prime in range(2, n+1): for number in test: if number == prime: continue elif prime%number == 0: count.append(1) else: count.append(0) if 1 not in count: prime_list.append(prime) count = [] return prime_list print(prime_finder(100))
def prime_finder(n): list_of_prime = [] for i in range(1, n+1): divisors = 0 for j in range(1, i+1): if i % j == 0: divisors += 1 if divisors <= 2: list_of_prime.append(i) list_of_prime.remove(1) return list_of_prime print(prime_finder(999))

My solution is definitely inefficient and inelegant, but I’m happy I got it done quickly

def prime_finder(n):
  result = []
  for i in range(2, n+1):
    counter = 0
    for j in range(2, i):
      if i%j == 0:
        counter += 1
    if counter == 0:
  return result
def prime_finder(n):
  primos = []
  for num in range(2,n+1):
    is_prime = True
    for div in range(2,num):
      if num % div == 0:
        is_prime = False
    if is_prime:
  return primos


Hi I was wondering if somebody could have a code check.

I am trying to solve the problem like this (I think I have seen more efficient solutions from your side but I would like to understand why mine isn’t working.

def prime_finder(n):
    prime_list = list(range(1,n+1))
    for num in prime_list:
        prime_slice = prime_list[:prime_list.index(num+1)]
        for x in prime_slice:
            if (num%x == 0) and ((x != num) and (x != 1) ):


The idea is to create a list of all numbers to the parameter number and delete the ones that are not primes.

It raises a ValueError in Jupyther

ValueError                                Traceback (most recent call last)
Cell In[31], line 13
     10                 continue
     11     print(prime_list)
---> 13 prime_finder(23)

Cell In[31], line 8, in prime_finder(n)
      6 num%x
      7 if (num%x == 0) and ((x != num) and (x != 1) ):
----> 8     prime_list.remove(num)
      9 else:
     10     continue

ValueError: list.remove(x): x not in list

Thanks a lot!


single line:
return [i for i in range(2, n + 1) if all(i % j != 0 for j in range(2, i))]

One-liner list comprehension

def prime_finder(n):
  primes = [i for i in range(2, n+1) if not any([i//j == i/j for j in range(2, int(i**0.5))])]
  return primes

The key here is using floor division. First, it’s easiest to start by typing out the nested for loop and append a boolean value to a list, then use any() to check if number n was divisible by any number less than itself. Then by comparing the result of the floor division to regualt division, we can confirm whether the n can be evenly divided by the testing number.

With a list comp. we can create the boolean list on the fly.

Now, to speed this up even more, we can only test n against every number less than or equal to sqrt(n) since n cannot be evenly divided by any number >sqrt(n). To do this we can change the range in the nested loop to int(i**0.5))

def prime_finder(n):
primes =
for num in range(2,n+1):
is_prime = True
for i in range(2,num):
if num % i == 0:
is_prime = False
if is_prime:
return primes


def prime_finder(n): prime_num = [] count = 0 for num in range(2, n + 1): for check in range(2, num): if num%check == 0: count += 1 if count == 0: prime_num.append(num) count = 0 return prime_num print(prime_finder(11))
def prime_finder(n): # Write your code here arr = [] arr1 = [] while (n >= 3): if n % 2 == 1: arr.append(n) n -= 1 for i in arr: for j in arr: if i == j: continue elif i % j == 0: arr1.append(i) arr2 = list(set(arr1)) for k in arr2: arr.remove(k) arr.reverse() arr.insert(0,2) return arr print(prime_finder(31))
def prime_finder(n): # Write your code here l=[] for x in range(2,n+1): c = 0 for _ in range(1,n+1): if x%_ ==0: c+=1 if c <= 2: l.append(x) return l print(prime_finder(11))
def prime_finder(n): list_of_numbers = list(range(2,n+1)) list_of_count_of_numbers = [] list_of_prime_numbers = [] for number in list_of_numbers: for element in range(2, n+1): if number % element == 0: list_of_count_of_numbers.append(number) for i in list_of_count_of_numbers: if list_of_count_of_numbers.count(i) == 1: list_of_prime_numbers.append(i) return list_of_prime_numbers print(prime_finder(11))