我需要使用 python 找到第 9 个正整数,它是 4、13、14、26 和 50 的倍数:
I need to find the 9th positive integer that is a multiple of 4, 13, 14, 26, and 50 using python:
为什么这段代码不起作用?我得到一个连续循环...
solution = 0
multiple = 0
max_bound = 100000
i = 1
while i < max_bound:
if (i % 4 == 0) and (i % 13 == 0) and (i % 14) and (i % 26 == 0) and (i % 50 == 0):
multiple += 1
if multiple == 9:
solution = i
break
i += 1
if multiple == 9:
print("#1 : 9th Multiple ::", "Correct." if solution == 81900 else ("Wrong: " + str(solution)))
您刚刚错过了 14 验证,您使用 (1 % 14)
而不是 (i % 14 == 0)
,其中 returns 0(False) 当数字可以被 14 整除而不是 True 并且使用 and 运算符时它不会进入 if 语句。
solution = 0
multiple = 0
max_bound = 100000
i = 1
while i < max_bound:
if (i % 4 == 0) and (i % 13 == 0) and (i % 14 == 0) and (i % 26 == 0) and (i % 50 == 0):
multiple += 1
if multiple == 9:
solution = i
break
i += 1
if multiple == 9:
print("#1 : 9th Multiple ::", "Correct." if solution == 81900 else ("Wrong: " + str(solution)))
为什么这段代码不起作用?我得到一个连续循环...
solution = 0
multiple = 0
max_bound = 100000
i = 1
while i < max_bound:
if (i % 4 == 0) and (i % 13 == 0) and (i % 14) and (i % 26 == 0) and (i % 50 == 0):
multiple += 1
if multiple == 9:
solution = i
break
i += 1
if multiple == 9:
print("#1 : 9th Multiple ::", "Correct." if solution == 81900 else ("Wrong: " + str(solution)))
您刚刚错过了 14 验证,您使用 (1 % 14)
而不是 (i % 14 == 0)
,其中 returns 0(False) 当数字可以被 14 整除而不是 True 并且使用 and 运算符时它不会进入 if 语句。
solution = 0
multiple = 0
max_bound = 100000
i = 1
while i < max_bound:
if (i % 4 == 0) and (i % 13 == 0) and (i % 14 == 0) and (i % 26 == 0) and (i % 50 == 0):
multiple += 1
if multiple == 9:
solution = i
break
i += 1
if multiple == 9:
print("#1 : 9th Multiple ::", "Correct." if solution == 81900 else ("Wrong: " + str(solution)))