Python Challenge - Prime Number Finder

Hey Codecademers,

I finished my prime number finder challenge & got it reviewed.
Please find my logic pasted below.
Please do comment & share your valuable inputs & suggestions to further optimize it, if possible.

def prime_finder(n):
try:
m = int(n)
if m <= 0:
print(‘Please enter a non-negative integer (natural number).’)
else:
remainder_list =
num_list =
for i in range(2, m+1):
for j in range(1, i+1):
rem = i%j
remainder_list.append(rem)
num_list.append(i)

  div_rema_list = [list(pair) for pair in zip(num_list, remainder_list)]

  div_rema_dict = {}
  for key, value in div_rema_list:
    if key in div_rema_dict:
      div_rema_dict[key].append(value)
    else:
      div_rema_dict[key] = [value]

  work_dict = {}
  for i, j in div_rema_dict.items():
    work_list = j[1:-1]
    work_dict[i] = [work_list]

  composite =[]
  prime = []
  for i, j in work_dict.items():
    k = j[0]
    if 0 in k:
      composite.append(i)
    else:
      prime.append(i)

return prime

except ValueError:
print(“Please enter the appropriate number.”)

print(prime_finder(11))

Why are the indentations not working here?

Because you did not format the code properly, try doing so in code-blocks of Python (by typing ```python`````), with your code inserted between python and the last backticks in a new line, the output should be like so:

# your code here
def prime_finder(n):
  try:
    m = int(n)
    if m <= 0:
      print('Please enter a positive integer (natural number).')
    else:
      remainder_list = []
      num_list = []
      for i in range(2, m+1):
        for j in range(1, i+1):
          rem = i%j
          remainder_list.append(rem)
          num_list.append(i)

      div_rema_list = [list(pair) for pair in zip(num_list, remainder_list)]

      div_rema_dict = {}
      for key, value in div_rema_list:
        if key in div_rema_dict:
          div_rema_dict[key].append(value)
        else:
          div_rema_dict[key] = [value]
        
      print(div_rema_dict)
    
      work_dict = {}
      for i, j in div_rema_dict.items():
        work_list = j[1:-1]
        work_dict[i] = [work_list]
      
      print(work_dict)
      composite =[]
      prime = []
      for i, j in work_dict.items():
        k = j[0]
        if 0 in k:
          composite.append(i)
        else:
          prime.append(i)

    return prime
  except ValueError:
    print("Please enter the appropriate number. Don't use quotation marks. Don't insert any alphabet ")

print(prime_finder(111))

Thanks you @ejini6969
Really appreciate your help.

Please find the formatted code presented in the reply before this.

def prime_finder(n): # Write your code here primeList = [] for i in range(2,n+1): divisible = False for j in range(2,i): if i % j == 0: divisible = True if divisible == False: primeList.append(i) return primeList print(prime_finder(11))
def prime_finder(n): prime = [] for x in range(2, n + 1, 1): prime_test, y = [], 2 while 2 <= y < x: if x % y == 0: prime_test.append(1) y += 1 if len(prime_test) == 0: prime.append(x) return prime
def prime_finder(n): primes = [] for num in range(1, n + 1): # all prime numbers are greater than 1 if num > 1: for i in range(2, num): if (num % i) == 0: break else: primes.append(num) return primes print(prime_finder(11))

This is what I ended up going for which I think is fairly solid

print(prime_finder(111111111))

A bit brutal, for a first test, but what the hey? It’s not that brutal, though it would have taken a lot of clock ticks, albeit.

We ran into an exception when running that code in the Codebyte shell.

Error: TypeError: Cannot read properties of undefined (reading 'length')

Perhaps as the author, you can track down the cause of this, it being solid and all? Have you run this code on your computer in IDLE?

Looking into it, didn’t run into any issues when testing it. I built it in pycharm rather than codecademy but didn’t test it on overly high values. Weird though can’t think of whats causing it off the top of my head as I’m not basing anything of the length of anything

1 Like

If you have IDLE, work in that environment. You can run it from the command line it you like, but I would just use the Shell. I’m not joining you in this, so let us know what surfaces.

def prime_finder(n):
  prime_numbers = []
  for num in range(1, n+1):
    count = 0
    for i in range(2, (num//2 + 1)):
        if(num % i == 0):
            count = count + 1
            break

    if (count == 0 and num != 1):
        prime_numbers.append(num)

  return prime_numbers

print(prime_finder(11))

My approach was for each number in the range from 1 to n+1 I checked if it is prime creating a loop that iterates through all the numbers from 2 to half of the current number plus 1. Thats because if there is a factor greater than half of the number, there will also be a factor less than half.
If the number was divisible by any number other than 1 and itself it’s not prime so i could move on to the next one.

If the number passes this test for all numbers smaller than itself, then it is considered prime and is added to the list prime_numbers.

I hope the explanation wasn’t confusing and makes sense.

Great deduction. One will find that the same can be said of the square root, which is in fact the maximum factor size of the lower half you mention above.

def prime_finder(n):
  # Write your code here
  o_list = []
  for r in range(1,n+1):
    j = 0
    for i in range(1,r+1):
      if r % i == 0:
        j += 1
      else:
        continue
    if j == 2:
      o_list.append(r)
  return o_list
print(prime_finder(11))



def prime_finder(n): primes = [] for i in range(2,n+1): notprime = False for j in range (2,i-1): if i%j == 0: notprime = True break if notprime == False: primes.append(i) return primes print(prime_finder(11))

def prime_finder(n):
if n < 2:
return None
if type(n) != int:
return “Not a valid entry.”
prime_list = [2]
for num in range(3, n + 1):
current = 2
for prime in prime_list:
if num % prime == 0:
current = prime
break
current = prime
if num % current != 0:
prime_list.append(num)
return prime_list

print(prime_finder(1223))

My solution:

def prime_finder(n): # Write your code here primes = [] for i in range(0, n + 1): flag = 0 if i < 2: continue if i == 2: primes.append(2) continue for x in range(2, i): if i % x == 0: flag = 1 break if flag == 0: primes.append(i) return primes print(prime_finder(11))

I used this:

My Solution:

def prime_finder(n): return [i for i in range(2, n+1) if all(i % k != 0 for k in range(2, i-1))] print(prime_finder(20))
def prime_finder(n): primes = [] #sets a empty list for x in range(2, n + 1): # we ignore 0 1 and 2 because primes and iterate through 2 to n primes.append(x) #add x to the prime list for y in range(2, x): #for every value of x check if prime if x % y == 0: #if its not prime primes.pop() #remove it from list as its the last entry safe to use pop break #break this loop and new x value return primes #return the list

prime_finder(11)