Python Challenge - Prime Number Finder

def prime_finder(n): primes = [] for n in range(2,n+1): divisors = [n/i for i in range(2,n) if n % i == 0] if len(divisors) == 0: primes.append(n) return primes print(prime_finder(11))
def prime_finder(n):
    prime = [2,3,5,7]
    return [num for num in range(1, n+1) if (num != 1) and ((num in prime) or (num%2 != 0 and num%3 != 0 and num%5 != 0 and num%7 != 0))]
 
print(prime_finder(11))

Straight forward for loops with 2 ways to increase computation speed. One is that the prime checker will only check until i//2+1 because it is useless to divide by >half of the number. Second is using ‘break’ to stop the for loop if we already know that the number is not a prime.

def prime_finder(n):

  primelst = []

  for i in range(2,n+1):
    div = 0
    for j in range(2,(i//2+1)):
      if i % j == 0:
        div += 1
      if div > 0:
        break
    if div == 0:
      primelst.append(i)

  return primelst
       
print(prime_finder(19999))

I think you need it to return an empty list if n ≤ 1

1 Like
def prime_finder(n):

#st1: putting the input for example 11 and all the numbers under it in list
    prime = list()
    input_list = list()
    result = list()
    for a in range(2, n+1):
        input_list.append(a)
#st2: finding the factors for each elements in input_list
        prime.clear()
        for i in range(1, a+1):
            if a % i == 0:
                prime.append(i)
    
#st3: check if the factors of that number is 2
        if len(prime) == 2:
            result.append(a)

            
                    
    return result
    
           


#final st: passing the value to the function
print(prime_finder(11))

def prime_finder(n):
  # Write your code here
  output = []
  lower = 2
  upper = n
  for num in range(lower, upper + 1):
    # all prime numbers are greater than 1
    if num > 1:
        for i in range(2, num):
            if (num % i) == 0:
                break
        else:
            output.append(num)
  return output
 
print(prime_finder(11))

:white_check_mark:

def prime_finder(n): arr = [] for i in range(n): is_prime = True for k in range(int(i+1/2)): if (is_prime): if (k+1 == 1): continue elif ((i+1) % (k+1) == 0): is_prime = False break if (is_prime and i+1 != 1): arr.append(i+1) return arr print(prime_finder(11))

#importing math function for use of ceiling function later on
import math

#defining a new function is_prime() which calculates whether the number is prime or not
def is_prime(n):
for i in range(2, math.ceil(n**0.5)+1):
if(n%i == 0):
return False

return True

def prime_finder(n):

For any number less than 2, just return an empty array

if n < 2:
return
else:
# Intialise an array with 2 because now it will always be a member of the array
primeli = [2]
for i in range(3, n+1): #start from 3 instead of 2 as it is already part of primeli
if(is_prime(I)): #call the is_prime() function
primeli.append(i)
return primeli

print(prime_finder(11))

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

def prime_finder(n):

Write your code here

prime=
for rangeNum in range(2,n+1):
isPrime=True
for j in range (2,rangeNum):
if rangeNum % j == 0 and rangeNum != j:
isPrime=False
break
if isPrime == True:
prime.append(rangeNum)
return prime

print(prime_finder(11))

def prime_checker(number):
  i = 2
  if number < 2:
    return False
  while i < number:
    if number % i == 0:
      return False
    i += 1
  return True

def prime_finder(n):
  lst = []
  for num in range(1, n+1):
    prime = prime_checker(num)
    if prime == True:
      lst.append(num)
  return lst
print(prime_finder(10))
def prime_finder(n): if type(n) is not int: raise TypeError("input must be an integer") if n == 0 or n == 1: return [] elif n == 2: return [2] list = [2] is_prime = False i = 3 while i <= n: x = 2 while x < i: if i % x == 0: is_prime = False break else: is_prime = True x += 1 if is_prime == True: list.append(i) i += 1 return list print(prime_finder(11))

this is very long with some unnecessary lines of code

Short and simple.

def prime_finder(n): lst = [] for num in range(2,n+1): if all(num%i!=0 for i in range(2,num)): lst.append(num) return(lst) print(prime_finder(11))
3 Likes
def prime_finder(n): # Write your code here primeNum=[] if n >=2: primeNum.append(2) for a in range(3,n+1): add=False for b in primeNum: if a%b == 0: add=False break else: add=True if add: primeNum.append(a) return primeNum print(prime_finder(30))
def prime_finder(n):
  def is_prime(x):
    return not any(x % y == 0 for y in range(2, x))
  return [x for x in range(2, n + 1) if is_prime(x)]
1 Like
def prime_finder(n):
  #create list from 2 to n
  twoToNList = []
  primesList = []

  for i in range(2, n+1):
    isPrime = True

    #case i = 2
    if i == 2:
      primesList.append(i)

    #other cases
    for j in primesList:
      remainder = i%j
      if remainder == 0:
        isPrime = False
        break

    if isPrime == True:
      primesList.append(i)
    
  return primesList
print(prime_finder(1))

Hopefully the naming is correct.

I used two ‘for’ loops twice with the ‘break’ to iterate through range numbers. I initially struggled with this because I didn’t break the loop.

def prime_finder(n): list_of_primes = [] for x in range(2,n+1): for y in range(2,x): if x % y == 0: break else: list_of_primes.append(x) return list_of_primes print(prime_finder(11))

nice use of list comprehension

def prime_finder(n): lst_to_remove = [] prime_num_list = [] for num in range(2, n+1): prime_num_list.append(num) lst_nums = [2,3,4,5,6,7,8,9] if num in lst_nums: lst_nums.remove(num) for div_num in lst_nums: if num % div_num == 0: lst_to_remove.append(num) break for remove in lst_to_remove: prime_num_list.remove(remove) return prime_num_list print(prime_finder(110))
def prime_finder(n): lst_to_remove = [] prime_num_list = [] for num in range(2, n+1): prime_num_list.append(num) lst_nums = [2,3,4,5,6,7,8,9] if num in lst_nums: lst_nums.remove(num) for div_num in lst_nums: if num % div_num == 0: lst_to_remove.append(num) break for remove in lst_to_remove: prime_num_list.remove(remove) return prime_num_list print(prime_finder(110))