Python Challenge - Prime Number Finder

def prime_finder(n):
“”"
Verilen bir sayıya kadar (dahil) olan tüm asal sayıları bulur.

Args:
n (int): Asal sayıların bulunacağı üst sınır.

Returns:
list: 1'den n'e (dahil) kadar olan asal sayıların listesi.
"""
primes = []  # Asal sayıları saklamak için boş bir liste oluşturun
for num in range(2, n + 1):  # 2'den n'e kadar olan her sayı için döngü
    # Herhangi bir sayı 2 ile karekökü arasındaki tüm sayılardan tam olarak bölünmüyorsa
    if all(num % i != 0 for i in range(2, int(num**0.5) + 1)):
        primes.append(num)  # Asal sayıları listeye ekleyin
return primes

Örnek kullanım

print(prime_finder(11)) # [2, 3, 5, 7, 11]

Post mine and ready to check cool answers from others

def prime_finder(n): # Write your code here prime_n = [2] # check for exception if type(n) != int: return 'you have to input only one integer.' elif n <= 1: return 'cannot find the prime numbers for less than 2.' elif n == 2: return prime_n else: # iterate devidend from 3 to n for d1 in range(3, n+1): # iterate devider from 2 to d1 for d2 in range(2, d1+1): # prime numbers only divisible by 1 and itself if d1 % d2 == 0 and d1 != d2: #print('{d1} % {d2} == 0 will break the loop'.format(d1=d1, d2=d2)) break elif d1 % d2 == 0 and d1 == d2: #print('find a prime: {d1}'.format(d1=d1)) prime_n.append(d1) return prime_n print(prime_finder(11)) for i in range(12, 25): print(prime_finder(i)) print(prime_finder(1)) print(prime_finder('x'))

Hi, just wanted to post my code! :upside_down_face:

def prime_finder(n): prime = [] isprime = "" for i in range(0, n + 1): if i == 2: prime.append(2) for j in range(2, i): if i % j != 0: isprime = "yes" else: isprime = "no" break if isprime == "yes": prime.append(i) return prime print(prime_finder(11))

Just to let others compare and take it as an example if suites better:

def prime_finder(n): prime_num = [] #Empty List for num in range(2, n+1): #Loop through the number in n, except 1 isprime = True # Create a boolean for check point for i in range(2, int(num ** 0.5) + 1): # Loop all possibilities of divisibles till the square root of num if num % i == 0: # check if num is divisible by i isprime = False # if divisible, set the breaker to False break # the loop stops if isprime: # if isprime is True the prime_num list will append num prime_num.append(num) return prime_num print(prime_finder(11))

def prime_finder(n):

#create a list
primes = []
# range 2-12
for num in range(2, n+1):
    for i in range(2, num):
        if (num % i) == 0:
            break
    else:
        primes.append(num)
return primes

print(prime_finder(11))

Just checking if a number is dividable by a prime number smaller than itself.
First i add every number to the prime_numbers and just remove it, when I find out it is no prime number

def prime_finder(n): # Write your code here prime_numbers = [] for i in range(2, n + 1): prime_numbers.append(i) for number in prime_numbers: if i != number and i % number == 0: prime_numbers.remove(i) break return prime_numbers print(prime_finder(11))

Hi this is my first time posting. Here is my solution. I decided to use recursion.

def prime_finder(n): # Write your code here def is_prime(n, checkpoint = 2): if checkpoint == n: return True elif n % checkpoint == 0: return False else: return is_prime(n, checkpoint + 1) return [x for x in range(2, n + 1) if is_prime(x) == True] print(prime_finder(11))
def prime_finder(n): prime_nums = [] worked_nums = [] for i in range(1, n+1): worked_nums = [] for j in range(1, i+1): if i % j == 0: worked_nums.append(j) if worked_nums == [1, i]: prime_nums.append(i) return prime_nums print(prime_finder(11))

Hi! Here is my try with this prime numbers problem.
First, I created a list with the numbers in the range we want to select the prime numbers.
Then I made the code loops through each number in this list and divide it by every number that came before. If this division is completed with no residue, it adds to a count of divisors of that number.
Finally, if the number has only 2 divisors (1 and itself), it is added to the list of prime numbers.

Made a test with large numbers and it worked really well. Tell me where you think I can do better!

def prime_finder(n): nums_in_range = list(range(1, n+1)) prime_numbers = [] for num in nums_in_range: new_range = list(range(1, num+1)) divisor_count = 0 for new_num in new_range: residue = num%new_num if residue == 0: divisor_count += 1 else: continue if divisor_count == 2: prime_numbers.append(num) return prime_numbers print(prime_finder(337))

Hi to everyone!! Firs time trying the challenges. Here´s my firs try to the first challenge.
Another things that could be included is a filter by type of input, like a text output if you input a text, or a float or a negative. I thik it could be easy adding some if, elif, else statements with a type() function. What do you think?

def prime_finder(n): prime = [] for i in range(1, n +1): divisores = [] for j in range(1, i+1): if i % j == 0: divisores.append(j) if len(divisores) == 2: prime.append(i) return prime print(prime_finder(11))

Hello! This is my code:

def prime_finder(n): result = [] for i in range(2, n+1): count = 0 for s in range(2, i+1): if i%s==0: count+=1 if count==1: result.append(i) return result print(prime_finder(100))

Here is my answer to the challenge. Any feedback would be great.

One area I am trying to get better at is adding better comments to my code

def prime_finder(n):
# Write your code here
  #define variables
  prime_numbers = [] #Empty list prime_numbers to be return at end of function
  num_list = list(range(3,n+1)) #list of all numbers from 3 up to n, inclusive
  
  #check if n is 1 or zero, return empty prime_number list, else append list with 2
  if n <= 1:return prime_numbers
  else: prime_numbers.append(2)
  
  #loop through all integers, i, in num_list
  for i in num_list:
    #loop through identified prime numbers
    for prime in prime_numbers:
      if prime <= i**(1/2):#check if prime number is less than square root of i

        #if prime number is factor of i break loop, goes to next i
        if i%prime == 0:break 
      
      #if loop through all prime numbers less than sqrt of i and no factors if i
      #were identified then i is a prime number and we append prime_numbers list
      else: 
        prime_numbers.append(i)
        break
  return prime_numbers
print(prime_finder(11))
def prime_finder(n): # function prime_numbers = [] # create a list to store the numbers for number in range(2, n + 1): # First for loop to loop through each number count = 0 # Creating a counter for test in range(1, n): # Second one to test if any other numbers divide to 0 if number % test == 0: # number divided by test with a remainder count += 1 # count only if the remainder is 0 if count <= 2: # check if the amount of numbers it can divide by is 2 or less prime_numbers.append(number) # add number to the list like a boss return(prime_numbers) # returning the list created at the start print(prime_finder(11)) # call the epic function

Here is my solution. I am nesting a function, is_prime. If the number is less than 2, the is_prime function returns False. If the number is 2, the is_prime function returns True. If the number is divisible by 2 and is greater than 2, the is_prime function returns False. Now concentrate on the odd numbers up to and including the square root of the number (to the integer). In the for loop of the is_prime function, if the number is divisible by the iterator, return False. If the odd number is not divisible by any of the iterator (3, 5, 7, 9, …) the is_prime function returns ‘True’. Now going back to prime_finder function. Define a list named primes. Iterate over the loop from 1 to n inclusive. If the iterator is a prime number, add the iterator to the list primes. Return the list primes.

def prime_finder(n):
  # Write your code here
  def is_prime(num):
    if num<2:
      return False
    if num == 2:
      return True
    if num%2  == 0 and num > 2:
      return False
    for i in range(3,int(num**0.5)+1,2):
      if num%i == 0:
        return False
    return True

  primes = []
  for i in range(1,n+1):
    if is_prime(i):
      primes.append(i)
  return primes
 
print(prime_finder(11))

Here is my attempt at solving the prime number finder challenge. I did this by using 2 for loops. One ran through all of the numbers, and the other ran through all of the possible division equations. If the modulus equation had no remainder, the counter z was incremented. All iterations where z = 2, are appended to the list of prime numbers.

def prime_finder(n):
  z = 0
  results = []
  for i in range (1, n+1, 1):
    for j in range (1, n+1, 1):
      if i % j == 0:
        z = z + 1
    if z > 3:
      z = 0
      continue
    elif z == 2:
      results.append(i)
      z = 0
    else:
      z = 0
      continue
  return(results)

print(prime_finder(11))

Hello there!
Here is my solution:

def prime_finder(n): # Write your code here prime_list = [] for num in range(1, n+1): prime = True for i in range(2,num): if num % i == 0: prime = False if prime and num != 1: prime_list.append(num) return prime_list print(prime_finder(11))

Hi!
My code:

def prime_finder(n): list_nums = range(2, n + 1) list_primes = [] for x in list_nums: list_val = [] for y in list_nums: if x % y == 0: list_val.append(x) if len(list_val) == 1: list_primes.append(x) return list_primes print(prime_finder(11))
def prime_finder(n):
    prime=[] #primes list creation
    for i in range(2,n+1):
      if i==2:
        prime.append(i) #first prime number
      else: 
        a=i-1
        while (i%a)!=0: #succession of divisions to search even only by itself, stops if result even by another number thzn itself
          if a==2:
            prime.append(i)
            break
          else:
            a=a-1
       
    return(prime)      
print(prime_finder(11))

def prime_finder(n): prime_list = [] for number in range(2, n+1): count = 0 for i in range(1, number+1): if number % i == 0: count += 1 if count == 2: prime_list.append(number) return prime_list print(prime_finder(11))