0
A problem with a generator of prime numbers
def prime_numbers(number): number_list = [] for i in range(1, number+1): for x in range(2, i): if i % x != 0: number_list.append(i) return number_list print( prime_numbers(30) ) So, I wanna generate prime numbers but the code works incorrectly
4 Answers
+ 4
You must keep count.
https://code.sololearn.com/cKi0NCC3dela/?ref=app
+ 5
Or you can add 'all'
for i in range(2, number+1):
if all(i % x != 0 for x in range(2, i)):
number_list.append(i)
+ 4
just wanna add that, you don't have to loop from 1->n, you can loop from 1->n/2 bcz the largest number that can divide any number is its half, and i suggest that you create a function isPrime(num) so it will make it easier and more readable :)
0
Oh OK, I understood you, thanks mate.