Created
July 3, 2012 01:11
-
-
Save davidnuon/3036809 to your computer and use it in GitHub Desktop.
One-liner in python to list all prime numbers from 2 to n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
primelist = lambda n : [x for x in xrange(2, n) if not 0 in map(lambda z : x % z, xrange(2,x))] | |
print ", ".join(map(str, primelist(100))) |
Why does it prompts
'function' object not iterable when I try to run this code?
More efficient:
primelist = lambda n : [x for x in xrange(2, n) if not 0 in map(lambda z : x % z, xrange(2, int(x**0.5+1)))]by Panna Lal Patodia
Sir, It's not printing the prime numbers. Also, can you explain how this code works, as in what is the logic behind this code?
For numbers in [item for item in range(2,50)]:
[len(x) for x in list(map(lambda x: [operator.mod(len(range(1,x)), z) for z in range(1,x)], [item for item in range(2,50)])) if x.count(0) == 2]
print(list(set([0 if([1 if(x%j==0) else 0 for j in range(2,x)].count(1)) else x for x in range(int(input("Enter the nth term: ")) +1 )]))[2:])
Hope this one helps
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More efficient:
primelist = lambda n : [x for x in xrange(2, n) if not 0 in map(lambda z : x % z, xrange(2, int(x**0.5+1)))]
by Panna Lal Patodia