简体   繁体   中英

How to write all those logical statements in a more efficient way in Python?

i = 20
while (i%2!= 0 or i%3!= 0 or i %  4 != 0 or i %  5 != 0 or
     i %  6 != 0 or i %  7 != 0 or i %  8 != 0 or i %  9 != 0 or
     i % 10 != 0 or i % 11 != 0 or i % 12 != 0 or i % 13 != 0 or
     i % 14 != 0 or i % 15 != 0 or i % 16 != 0 or i % 17 != 0 or
     i % 18 != 0 or i % 19 != 0 or i % 20 != 0):
i+=20
print(i)

I want to find LCM of numbers ranging from 1 to 20. This code in itself is efficient (faster than alternatives), but is there any way that computer can recognize the pattern and make all logical statements accordingly?

您可以使用any

while any(i % j != 0 for j in range(2, 21)):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM