Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

I am on pset2a, so I am trying to find the highest value of n that doesn't have a solution to the diophantine equation. What I am struggling with is finding all possible solutions (values of n) to the diophantine equation, where 6a+9b+20c = n. Any help is appreciated!

OpenStudy (anonymous):

Do you mean you're having trouble doing the math or you're having trouble turning the math into a program? If you're having trouble turning it into a program, I recommend you break it down. Don't find ALL the solutions, just look for the first few solutions. For instance, the smallest number of nuggets you can buy with those combinations is 6, right? 6*1 +9*0+20*0=6. So how can you write a program to spit that out? Then write a program that will test "a" iterating through a short list (like 1 to 5). Keep adding to the program until you end up with the answer the problem is looking for.

OpenStudy (anonymous):

Awesome thanks! Now if I want to test values of a,b, and c from 0 to 5 lets say, would the best way to do that be with 3 nested For Loops?

OpenStudy (anonymous):

@dvena, That's how I would do it. I'd recommend just detecting if a single value has a solution. If you can't figure out if a single value has a solution, then you have no chance of testing multiple values, no matter how few values we're talking about. The main challenge on this problem, I think, is writing the 3 nested loops, doing the right test, and remembering the result when the 3 loops exit. With the primes question you could break out of the loop when you found a divisor. Here there's no clean way to break out of 3 loops at once, so just "ride them out" and efficiency be damned! Once you get this part working solidly, then you can start thinking about testing multiple values.

OpenStudy (anonymous):

I am stuck on testing the multiple values. As i understand, what I want to do, is after running through each value of n I want to check if it is equal to some number (x) between 1 and 50 (i chose 50 because at this point I know 50-55 nuggets is possible. If not, I want to print that number. I'm struggling with how to implement that test after each value of n.

OpenStudy (anonymous):

so far this is what I have: http://codepad.org/HMDBBoau I am getting an infinite loop with it though.

OpenStudy (anonymous):

You're on the right track. The simple stuff first: add comments to your code. I took your post and added lots of comments to explain what each line is supposed to do. Makes life easier for everyone involved. Next, you had said: n = 6*a + 9*b + 20*c That's a common mistake. You don't want to assign "n" the value of the equation, which is what = does. You want to test to see if the equation is true, which is what == does. So you need to say: n == 6*a + 9*b + 20*c I don't think x is working the way you want it to. I removed it from the code so that you could see what's going on without it. You'll see from the code output that n has to be defined in some way. So if you're interested in 50-55 nuggets, then maybe think about that range for n, and where that could go in the code. http://codepad.org/96zJ9Lig

OpenStudy (anonymous):

Very helpful. Thanks again.

OpenStudy (anonymous):

I am now looking for the highest number of nuggets that cannot be bought in exact quantity in groups of 6, 9, and 20. This is what I have so far, I know that it is 43. But I don't know how I am supposed to automatically detect 44-49 and then save 43. Any help is appreciated. http://codepad.org/wEZek9OY

OpenStudy (anonymous):

one way would be to have another variable that keeps track of how many in a row have a solution. then when that variable equals 6 you know that you are done

OpenStudy (anonymous):

Just a little detail. Your code will detect 43 because you know that's the answer. But if you didn't know beforehand that it was 43 you would want to turn the outer loop (that loops over values of n) into a while loop that increments n indefinitely. The, in the a, b, and c for loops, you would want to set upper limits for those variables appropriately (e.g. what's the maximum number of boxes of 6 you would need to consider?). But don't worry about this stuff until you get it working properly. Then save a backup of your code before breaking something that already works.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!