+ 3

is it possible? list comprehension for primes

Dear fellow learners, I currently go through the "more types| 6/9 list comprehension" module and i was wondering if you could make a list full of primes. The problem, that i ran into, is that I think you need a second variable for it in order to work. This is what I came up with. primes=[i == i for i in range(20) if i % x > 0 for x in range(20)] However, it produces: UnboundLocalError: local variable 'x' referenced before assignment. Any ideas? I'm not very experienced and grateful for every suggestion. Thank you

3rd Sep 2018, 6:58 AM
Joseph.Atzinger
11 Respostas
+ 12
I just came up with this primes = [i for i in range(2,100) if all(i%x != 0 for x in range(2,i))] It works but I'm not sure if I can explain how lol đŸ€Ł
3rd Sep 2018, 7:51 AM
Eduardo Petry
Eduardo Petry - avatar
+ 11
HonFu Hahahah the devil is in the details I guess 😅 and yes, math is really important, mostly in more complex algorithms. Don't worry, though -- everything you learn accumulates over time, and that's what makes this app so great! Learning is an ability with no limitations and each person has their own path. The community here at SL is full of supportive people and I love seeing the desire to learn in others. We all help each other improve! 😊
3rd Sep 2018, 9:16 AM
Eduardo Petry
Eduardo Petry - avatar
+ 11
HonFu The code playground here in SL works in mysterious ways 😂 One thing I'd like to point out (but I'm not 100% sure if I'm right): calculating the sqrt and storing it in a variable, then using the variable in the `range` function is probably more efficient. Because then the calculation is done only once and is reused from the variable. I'm not sure how the `range` function works internally, but it happens with `for` loops on C-like languages. The calculation of the stop condition keeps on being made at every iteration of the loop and that slows the code down!
3rd Sep 2018, 9:54 AM
Eduardo Petry
Eduardo Petry - avatar
+ 10
HonFu In fact, it'd be even better to check up to `int(i**0.5)+1`, but I wasn't concerned with optimization.
3rd Sep 2018, 8:11 AM
Eduardo Petry
Eduardo Petry - avatar
+ 9
HonFu int(i**0.5) != i//2 `i**0.5` is the square root of `i`. When checking if a number is prime, it is enough to check up to the square root of the number. đŸ‘đŸ»đŸ˜ Doing this lowers the time complexity to O(n^(1/2)), which is a nice improvement.
3rd Sep 2018, 8:29 AM
Eduardo Petry
Eduardo Petry - avatar
+ 5
You can also use filter with lambda with Eduardo Petry's way print(list(filter(lambda x: all(x%i != 0 for i in range(2, x)), range(2,100))))
3rd Sep 2018, 12:19 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 3
Aaaah ... damn, I think I always read int(i*0.5)+1! (I still do that with '=' and '==', too, I think it's my most frequent SyntaxError.) Interesting, I didn't know about the square root method; almost every day here on sololearn I experience how a lack of math skill negatively impacts my coding. It's a good thing I came here so you can all help me out with improving on that! :)
3rd Sep 2018, 9:03 AM
HonFu
HonFu - avatar
+ 2
Eduardo Petry, would it be quicker if you wrote 'i//2+1' in the end? I think you don't need to check further, right?
3rd Sep 2018, 8:05 AM
HonFu
HonFu - avatar
+ 2
Eduardo Petry, I always wanted to ask why people were using the int(i**0.5) syntax. Can you explain why it would be better? If you need an int anyway, why not go with floor div from the start? (Sorry for the OT.)
3rd Sep 2018, 8:27 AM
HonFu
HonFu - avatar
+ 2
I just compared the two methods with time.process_time, and the sqrt way was clearly quicker. Nevertheless, when I increased the range, the point of 'Time limit exceeded' was exactly the same! *scratches head*
3rd Sep 2018, 9:44 AM
HonFu
HonFu - avatar
+ 1
I made a code to find a chosen amount of primes in an efficiënt way. idk if it helps but here you go. https://code.sololearn.com/cXP80RNTKJOs/?ref=app
13th Jun 2019, 9:00 PM
Max Poppe
Max Poppe - avatar