Jan-28-2022, 02:25 PM
Hello,
Its probably the wrong way to do this, but i am trying to print my list and because of the if statement to get rid of the false returned, i ended up with doubles in my list.
How to avoid this?
Of course the Sieve of Eratosthenes is more efficent. The question is just about controling the return and indentation.
Thank you
Its probably the wrong way to do this, but i am trying to print my list and because of the if statement to get rid of the false returned, i ended up with doubles in my list.
prime = [] def is_prime_v1(n): if n == 1: return False for d in range(2, n): if n % d == 0: return False prime.append(n) return n for n in range(1,21): if is_prime_v1(n) != False: print(is_prime_v1(n)) print(prime)
Output:2
3
5
7
11
13
17
19
[2, 2, 3, 3, 5, 5, 7, 7, 11, 11, 13, 13, 17, 17, 19, 19]
>
print(prime) seems to be prisonner of the for indent. It cause the numbers to repeat twice. How to avoid this?
Of course the Sieve of Eratosthenes is more efficent. The question is just about controling the return and indentation.
Thank you