Skip to content

Instantly share code, notes, and snippets.

@davidnuon
Created July 3, 2012 01:11
Show Gist options
  • Save davidnuon/3036809 to your computer and use it in GitHub Desktop.
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
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)))
@pannalal
Copy link

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

@RR93-user
Copy link

Why does it prompts
'function' object not iterable when I try to run this code?

@KKrish1624
Copy link

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?

@guillardi
Copy link

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]

@SATYADAHAL
Copy link

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