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.

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