python - 在Python中求解三变量丢番图方程

标签 python algorithm diophantine

我是 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/

相关文章:

python - 如何使用 "onehot_enc"实现朴素贝叶斯算法?

python - 在输入中找到相同的数字并对成对的数字求和

近似整数分配问题最优解的算法

python - 如何找到多面体/多面体内的整数点(坐标)?

algorithm - 欧拉计划问题 233

Python 字节文字

Python pexpect 返回命令和命令的输出

python - 如何将单个电位计值转换为 R、G、B?

algorithm - 反复消除完美平方后剩下多少?

algorithm - 有M条赛道的N匹马,如何判断排名?