Hello everyone! I´ve just started covering some python essentials, though I could not wait to tackle some of the coding challenges. There are surely better and more straightforward ways of tacking the challenge, but who knows… Maybe someone chose the same path as me and so this could be helpful.

I made my solution only checking with prime’s numbers (not all other numbers) & only check odd numbers, this must be faster than other aproachs.

My solution, using the Eratosthenes algorithm.

```
[codebyte]
[/python3]
type or paste code here
def prime_finder(n):
primes = [True] * (n + 1)
primes[0] = primes[1] = False
for num in range(2, int(n**0.5) + 1):
if primes[num] == True:
for multiple in range(num * num, n + 1, num):
primes[multiple] = False
prime_numbers = [num for num, is_prime in enumerate(primes) if is_prime == True]
return prime_numbers
print(prime_finder(11))
```

The original poster’s basically saying that the default is that the number isn’t a prime. Hence, p = False. Now, if the original number is divided by some number and the remainder after division is zero, it automatically means the original number is *not* a prime: so p = True. In Python:

```
if num % y == 0:
p = True
```

I believe that the OP could make p = True as default. and then ask:

```
if p:
list_of_primes.append(num)
```

def prime_finder(n):

# Write your code here

prime_numbers =

for num in range(n+1):

if num > 1:

for i in range(2, num):

if (num % i) ==0:

break

else:

prime_numbers.append(num)

return prime_numbers

print(prime_finder(11))

```
def prime_finder(n):
i=2
primes=[]
while i<=n:
no_primes=[]
for j in range(2,i):
if i%j==0:
no_primes.append(i)
if len(no_primes)==0:
primes.append(i)
i+=1
return primes
print(prime_finder(51))
```

I used a lambda function to solve this challenge. The reason for using `num**0.5`

(square root of the number) is for optimization. If a number doesn’t have a divisor smaller than its square root, then it won’t have any divisors larger than its square root either.

My solution is definitely inefficient and inelegant, but I’m happy I got it done quickly

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

```
def prime_finder(n):
primos = []
for num in range(2,n+1):
is_prime = True
for div in range(2,num):
if num % div == 0:
is_prime = False
break
if is_prime:
primos.append(num)
return primos
print(prime_finder(11))
```

Hi I was wondering if somebody could have a code check.

I am trying to solve the problem like this (I think I have seen more efficient solutions from your side but I would like to understand why mine isn’t working.

```
def prime_finder(n):
prime_list = list(range(1,n+1))
for num in prime_list:
prime_slice = prime_list[:prime_list.index(num+1)]
for x in prime_slice:
num%x
if (num%x == 0) and ((x != num) and (x != 1) ):
prime_list.remove(num)
else:
continue
print(prime_list)
prime_finder(7)
```

The idea is to create a list of all numbers to the parameter number and delete the ones that are not primes.

It raises a ValueError in Jupyther

```
ValueError Traceback (most recent call last)
Cell In[31], line 13
10 continue
11 print(prime_list)
---> 13 prime_finder(23)
Cell In[31], line 8, in prime_finder(n)
6 num%x
7 if (num%x == 0) and ((x != num) and (x != 1) ):
----> 8 prime_list.remove(num)
9 else:
10 continue
ValueError: list.remove(x): x not in list
```

Thanks a lot!

Alberto.

single line:

return [i for i in range(2, n + 1) if all(i % j != 0 for j in range(2, i))]

## One-liner list comprehension

```
def prime_finder(n):
primes = [i for i in range(2, n+1) if not any([i//j == i/j for j in range(2, int(i**0.5))])]
return primes
```

The key here is using **floor division**. First, it’s easiest to start by typing out the nested `for`

loop and append a `boolean`

value to a list, then use `any()`

to check if number `n`

was divisible by any number less than itself. Then by comparing the result of the **floor division** to regualt division, we can confirm whether the `n`

can be evenly divided by the testing number.

With a **list comp.** we can create the boolean list on the fly.

**UPDATE**:

Now, to speed this up even more, we can only test `n`

against every number less than or equal to * sqrt(n)* since

`n`

cannot be evenly divided by any number `>sqrt(n)`

. To do this we can change the range in the nested loop to `int(i**0.5))`

def prime_finder(n):

primes =

for num in range(2,n+1):

is_prime = True

for i in range(2,num):

if num % i == 0:

is_prime = False

break

if is_prime:

primes.append(num)

return primes

print(prime_finder(11))