我是 Python 的初学者,并试图采取 MIT 6.00 ,提供的页面是作业页面。
我在 assignment 2 ,在那里我必须找到丢番图方程的解,我的数学真的不是那么好,所以我试图尽可能多地理解它的作用,并想出一个解决方案。
这是我要做的:
def test(x):
for a in range(1,150):
for b in range(1,150):
for c in range(1,150):
y = 6*a+9*b+20*c
if y == x:
print "this --> " , a, b, c
break
else : ##this to see how close i was to the number
if y - x < 3:
print a, b, c , y
作业指出 50, 51, 52, 53, 54, and 55
有一个解决方案, 但不幸的是脚本只得到 50, 53 and 55
的解决方案.
如果有人解释了我的代码中的错误,或者我根本不理解丢番图方程,我将不胜感激,请告诉我它的全部内容以及如何找到解决方案,因为我不能将作业的解释记入我的脑海。
谢谢。
最佳答案
作业说:
To determine if it is possible to buy exactly n McNuggets, one has to solve a Diophantine equation: find non-negative integer values of a, b, and c, such that 6a + 9b + 20c = n.
看来您必须在函数范围内包含零。这样,您就可以找到所需的所有数字的解决方案。
关于python - 在Python中求解三变量丢番图方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12584993/