More Succint Code for Prime Numbers

 It's 12:55 midnight, I was working on a problem statement of Euler Project+, and I just got an idea to make the algo for finding the prime number even more efficient than the one I made by returning the list of bools to check if the number is prime or not. This time I would not be using a separate function for checking that but instead I will use 2 list comprehensions at a time, and will print the numbers by using the first list made inside another list for every number.


n=int(input("Enter the number till which you want the prime numbers: "))
ls=[print(i) for i in range(2,n+1) if i not in [i for i in range(2,n+1) for j 
in range(2,i) if i%j==0]]

Comments

Popular Posts