Python Challenge - Prime Number Finder

This community-built FAQ covers the “Prime Number Finder” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Python challenge Prime Number Finder

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

def prime_finder(n): #take care of edge cases 0 - 4 to make loop easier if n < 1: return [] if n == 2: return [2] if n < 5: return [2, 3] #check n by multiples of 6 +- 1 #against primes already found prime_list = [2, 3] x = 5 toggle = True while (x < n+1): prime = True for y in prime_list: if x%y == 0: prime = False break if prime: prime_list.append(x) if toggle: x += 2 else: x += 4 return prime_list print(prime_finder(11)) print(prime_finder(964))

I believe this is fairly solid, but there’s probably quicker ways to ‘sieve’ out all the primes.
I think every time I try this challenge I end up doing it slightly differently.

def prime_finder(n): list_of_primes = [] for x in range(2, n+1): num = x p = False for y in range(2, num): if num % y == 0: p = True break if p: continue else: list_of_primes.append(num) return list_of_primes print(prime_finder(11))
def prime_finder(n): # Write your code here count = [] for i2 in range(1,n): num = 0 for i in range(2,n+1): if (((i2+1)%i) == 0) and (i != (i2+1)): num += 1 if num ==0: count.append(i2+1) return count print(prime_finder(11))
import math def is_prime(n): if n <= 1: return False for i in range(2, int(math.sqrt(n)) + 1): if n % i == 0: return False return n def prime_finder(n): return [is_prime(i)for i in range(2,n+1) if is_prime(i) != False] print(prime_finder(11))
def prime_finder(n): answer = [] for x in range(2, n+1): prime = True for i in range(2, x): if x != i and x%i == 0: prime = False if prime: answer.append(x) return answer print(prime_finder(11))
def prime_finder(n): # Write your code here primes = [] for i in range(1, n+1): if i > 1: num = i for j in range(2, int(num/2)+1): if num % j == 0: break else: primes.append(num) return primes print(prime_finder(11))

It’s not necessary to check the division by more than half of the number to check.

def prime_finder(n): primes = [2] for i in range(3, n + 1): prime_count = 0 half = int(i / 2) for j in range(2, half + 1): if i%j == 0: prime_count = 1 if prime_count == 0: primes.append(i) return primes print(prime_finder(11))
def prime_finder(n):
  # Write your code here
  prime_list = []
  for x in [*range(2,n)] + [n] if n >= 2 else [*range(2,n)]:
    flag = False
    for i in range(2,x):
      if x % i == 0:
        flag = True
        break
    if not flag:
      prime_list.append(x)
  return prime_list
print(prime_finder(1213))
def prime_finder(n):
  primes=[]
  for i in range (2,n+1):
    check=True
    for prime in primes:
      if prime > i**(.5):
        break
      if i%prime ==0:
        check=False
    if check==True:
      primes.append(i)
  return primes

print(prime_finder(11))
def prime_finder(n): z=[2] for i in range(1,(n+1)): for a in range(2,i): if i%a==0: break else: pass if a==(i-1): if i not in z: z.append(i) return z print(prime_finder(11))

My solution code:

def prime_finder(n): # Write your code here primes = [] for num in range(1, n+1): divisors = [] for div in range(1, n+1): if num%div == 0: divisors.append(div) if divisors == [1, num]: primes.append(num) return primes print(prime_finder(11))
def prime_finder(n): not_primes = [] candidates = [i for i in range(2, n+1)] for num in range(n+1): for factor in range(2, num): if num % factor == 0 and num not in not_primes: not_primes.append(num) break for num in not_primes: candidates.remove(num) return candidates
def prime_finder(n): return_list = [] primes = [True for i in range(n+1)] for i in range(2, n): if primes[i] == True: for j in range(i*i, n+1, i): primes[j] = False for i in range(2, n+1): if primes[i] == True: return_list.append(i) return return_list print(prime_finder(11))
def prime_finder(n): prime_numbers=[] for number in range(n+1): contador=0 for divisor in range(1,number+1): if number%divisor==0: contador+=1 if contador==2: prime_numbers.append(number) return prime_numbers print(prime_finder(11))

You can go one step further, you only need to check up to sqrt(n) + 1

import numpy as np is_prime = lambda n : ~np.any(n%np.array(list(range(2,n))) == 0) prime_finder = lambda n : [i for i in range(2,n+1) if(is_prime(i))]
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(11))
def prime_finder(n): # Write your code here primes = [] for i in range (2, n+1): for j in range(2, i): if i % j == 0: break else: primes.append(i) primes.sort() return primes print(prime_finder(11))
def prime_finder(n): # Write your code here ret = [] if n == 1: return ret else: for i in range(n-1): i = i+2 p = True for t in range(i-2): t = t+2 if i % t == 0: p = False break if p: ret.append(i) return ret print(prime_finder(40))