Rafael Adel Rafael Adel - 1 month ago 11
Python Question

Solving three variables Diophantine equation in Python

I'm a beginner in Python, and tried to take MIT 6.00, the page provided is the assignments page.

I'm at assignment 2, where i have to find a solution for Diophantine equation, i'm really not that great in math, so i tried to understand what it does as much as i can, and think of a solution for it.

Here's what i got to :

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


The assignment states that there's a solution for
50, 51, 52, 53, 54, and 55
, but unfortunately the script only gets the solution for
50, 53 and 55
.

I'd be very grateful if someone explained what's wrong in my code, or if i'm not understanding Diophantine equation at all, please tell me what is it all about and how to find a solution for it, since i cant get the assignment's explanation into my head.

Thanks.

aga aga
Answer

The assignment says:

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.

It seems that you have to include zero in the ranges of your function. That way, you can find solutions for all the numbers you need.