Testing divisibility by multiple numbers

后端 未结 5 1619
灰色年华
灰色年华 2021-01-13 03:46

In Python, is there a way to test if a number is divisible by multiple numbers without writing out the modulo operation for each factor?

More specifically, is there

5条回答
  •  隐瞒了意图╮
    2021-01-13 03:49

    if all(i % n == 0 for n in reversed(xrange(11, 101))):
        print(i)
    

    Only a slightly modified version of the duplicate answers already given: xrange returns an object that generates the numbers in the range on demand (slightly faster than range and more memory efficient). If performance is important here, which it often is for mathematical code snippets like this bit, the reverse iterator will check the bigger numbers first. This is more likely to kick you out of the all() function sooner.

提交回复
热议问题