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):
  # Write your code here
  res = []
  IsPrime = [True] * (n + 1)
  IsPrime[0] = False
  IsPrime[1] = 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):

Write your code here

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))

Snimak ekrana 2022-06-24 221141

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))
def prime_finder(n): # Write your code here prime = [] if n >= 2: prime.append(2) if n >= 3: prime.append(3) if n > 4: for p in range(5, n + 1): is_prime = True for divisor in range(2, int(p/2)): if p % divisor == 0: is_prime = False break if is_prime: prime.append(p) return prime print(prime_finder(11))
def prime_finder(n):
  # Write your code here
  l = []

  for i in range(2,n+1):
    if (i==2 or i==3 or i==5 or i==7):
      l.append(i)
    
    elif i%2 != 0 and i%3 != 0 and i%5 !=0 and i%7 != 0:
      l.append(i)
    
  return l

#print(prime_finder(100))
def prime_finder(n):  
  prime_numbers_before_n = list(range(2,n+1))
  for i in range(1,n+1):
    for j in range(1,n+1): 
      if i % j == 0 and j != 1 and i != j:
        prime_numbers_before_n.remove(i)
        break
  return prime_numbers_before_n

print(prime_finder(11))

def prime_finder(n):

Write your code here

i = 0
num_list =
prime_list =
non_prime_list =

for num in range(1, n+1):
num_list.append(num)

for item in num_list:
position = num_list.index(item)
# preceeding_num = num_list[position-1]
preceeding_num_list = num_list[1:position]
if position==0:
pass
else:
for num in preceeding_num_list:
if item%num==0:
if item in non_prime_list:
pass
else:
non_prime_list.append(item)
num_list.remove(1)
for number in num_list:
if number in non_prime_list:
pass
else:
prime_list.append(number)
return(prime_list)

print(prime_finder(11))

#This was my solution for the prime number finder. What do you guys think? Constructive criticisms are welcomed. Have a nice day!

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

This is my solution. Any feedback is welcome. Thank you guys! Have a nice day!

def prime_finder(n): num = 2 prime_nos=[] counter=0 while num!=n+1: for i in range(num): if num%(i+1) == 0: counter+=1 if counter<=2: prime_nos.append(num) counter=0 num+=1 return prime_nos print(prime_finder(11))
import unittest def prime_finder(n): # mod 2 == 1 prime_list = [] is_prime = 1; for i in range (2, n+1): for j in range (2, 10): if (i % j == 0) and (i != j) : is_prime = 0 break if (j == i): break if is_prime: prime_list.append(i) is_prime = 1 return prime_list class TestPrime(unittest.TestCase): def test_prime(self): onehundred = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] self.assertEqual(prime_finder(100), onehundred) unittest.main(exit=False)