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
  • 774
  • 1
  • 11
  • 24

2 Answers2

8
if any(n % x == 0 for x in mylist):
    print "Not Prime"
jamylak
  • 120,885
  • 29
  • 225
  • 225
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
  • 313,408
  • 56
  • 485
  • 544