3

I was wondering whether you could check a number against all numbers in a list, for example:

if n % mylist == 0:
    print "Not Prime"

And if you're wondering, this is a continuation of this question, I hope there's not any rules about question frequency... :/

Community
  • 1
  • 1
cjm
  • 814
  • 1
  • 11
  • 26

2 Answers2

8
if any(n % x == 0 for x in mylist):
    print "Not Prime"
jamylak
  • 128,818
  • 30
  • 231
  • 230
5

... or even shorter:

if not all(n % x for x in mylist):
    print "Not prime"

(although I'd prefer jamylak's version - explicit is better than implicit)

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561