有一盒糖果:
1个1个取,正好取完;
2个2个取,还剩1个;
3个3个取,正好取完;
4个4个取,还剩1个;
5个5个取,还差1个;
6个6个取,还剩3个;
7个7个取,正好取完;
8个8个取,还剩1个;
9个9个取,正好取完。
请问:这个盒子里至少有多少个糖果?
循环语句
条件虽然多,但其实是重复的,比如2和4都是2 的倍数,只要计算2个2个取,还剩1个就行了,如果全部条件的话,实现代码如下
index = 1
while 1:
if index % 2 == 1 and index % 3 == 0 and index % 4 == 3 and index % 5 == 4 and index % 6 == 3 and index % 7 == 0 and index % 8 == 7 and index % 9 == 0:
print(index)
break
index += 1
def candy():
i = 0
while 1:
i += 1
if not ((i+1)%5 or i%7 or (i-1)%8 or i%9):
return i
>>> candy()
1449
>>>