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

Would someone mind breaking down the math for problem set 2 Question 1-3-4 for me? I wrote this program http://pastebin.com/u4Dac2gK thinking that it just wanted to know if the number given was a solution or not but it seems that I was mistaken. The part I am not understanding is the math, so I do not know how to map the problem into python.

OpenStudy (anonymous):

looks like you have the math correct you 'generate' different combinations of i, j, k and use those variables in the nugget equation and compare the result to the number you are looking for. have you tried to write out in words what you are trying to do? that usually helps. ine 19: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#testing-for-truth-values your code looks like it is doing lots of stuff - can you walk us through what it does (generally)?

OpenStudy (anonymous):

Yeah sure I would be happy to. I am not having a problem with the code itself, I am having a problem working with the question. Maybe the math is over my head, but I dont understand what exactly the goal for each question is. Breaking the code down step by step, I first take the input for any number of nuggets, which I will test to see if it is possible to generate somehow. The next thing I do is basically solve for each variable while all other variables equal zero. something like 6(a) +9(0) + 20(0) = N... From that I can determine the maximum possible amount of 1 package which can reach the specified N from the user. Then I repeat this for the other two variables to get the maximum number of boxes for each user input N. From here I have my baseline, 0 0 0, and my maximum values, atop btop ctop, from which I will generate all possible permutations to see if N is possible. If it is possible, foundCombination is set to true, and the user interfaces with the program to either find out that the program has found a match or it has not. Then the user decides whether they would want to view all possible combinations or not by selecting Y/N which sets the while loop rerun either to true or false to run again. Then all of the permutations are printed out for the user to view. All the while I have a thing called a globalCounter that I am using to control the flow of the program. While it is running, I assign different values to globalCounter at specific times to get different blocks of code to be carried out each time the rerun loop runs. By the second time, the program cuts out all of the code using the globalCounter except for the code that generates all possible permutations and then prints it out. Then the program exits. If it was hard to follow I am sorry, but if you run the code you should understand it pretty quickly. Like I said though, I am not having much of a problem with the coding aspect now, I am having a problem understanding what exactly the question is asking to do, and then mapping that to the program. Unless what I did is what they are asking for, but I do not think this is the case. Sorry for the confusion and let me know if I can clear anything up.

OpenStudy (maitre_kaio):

Your code is probably right, but I think your solution is unduly complicated. For example, the whole code searching for atop, btop, ctop could be replaced by three lines; atop = totalNugs / 6 btop = totalNugs / 9 ctop = totalNugs / 20 After that, you're doing very intricated things to find all the possible combinations to by totalNugs. It's not in the assignment. The question is 'is it possible ?'. Period. So first you should simplify the code to test if a given number satisfies the constraints. After that, write code that walks through all integers, when it finds 6 consecutive integers ok -> it is done and prints the last nok integer.

OpenStudy (anonymous):

I believe you are correct about the complicated code, it probably could have been written much easier, as you showed me in three lines. The last thing you said, "when it finds 6 consecutive integers ok -> it is done and prints the last nok integer." This is the part I am not understanding. What does this have to do with finding whether not a given number is achievable with some combination of the three packages? Are you saying that after a certain number, all past that number are achievable?

OpenStudy (maitre_kaio):

That's where the theorem and the problem 2 enter the scene. The thorem says that if we can x such as it is possible to buy x, x + 1, ..., x + 5 nuggets, then it is possible for any integer >= x. Another way to say it is if we find 6 consecutive integers satisfying the requirements, then it is possible for all the larger integers.

OpenStudy (anonymous):

I understand what it means now, that's what I was having problems with. So essentially anything over the number 50 will have a possible solution. Interestingly enough, using my program that calculates all possible solutions, I actually came up with a graph depiction of the theorem. It turns out that around 2250 nuggets to test, it is possible to get more solutions than the given number and then sky rockets into 10's of thousands of solutions... See the attachment following this post. It shows from 500 - 10,000 the possible solutions there are to make the given N, and then plots it out in a graph which looks to be getting exponentially bigger... If you are interested.

OpenStudy (maitre_kaio):

It is a classic result, since the number of combinations grows exponentially with the number. Nice work to show it visually.

OpenStudy (anonymous):

Thanks for the help maitre_kaio

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!
Latest Questions
Austinsbabygirl4life: Do you think guys would survive on periods?
1 hour ago 22 Replies 2 Medals
alyviahwillis: if anyone has any questions feel free to DM me... I don't mind at all.
3 hours ago 0 Replies 0 Medals
Rosee5656: how do I level up!??!? >,
2 hours ago 14 Replies 4 Medals
Abby13915: I made an alpaca for my fashion club
7 hours ago 3 Replies 0 Medals
notmeta: help
9 hours ago 54 Replies 1 Medal
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!