[Tutor] Problem

2016-08-28 Thread shahan khan
Hello
I'm teching myself Python using MIT opencourse ware. I'm a beginner and
have some what knowledge of c and c++. I'm using Python version

Here is the problem:
McDiophantine: Selling McNuggets
In mathematics, a Diophantine equation (named for Diophantus of Alexandria,
a third century Greek mathematician) is a polynomial equation where the
variables can only take on integer values. Although you may not realize it,
you have seen Diophantine equations before: one of the most famous
Diophantine equations is:
xn + yn= zn.
For n=2, there are infinitely many solutions (values for x, y and z) called
the Pythagorean triples, e.g. 32 + 42 = 52. For larger values of n,
Fermat’s famous “last theorem” states that there do not exist any positive
integer solutions for x, y and z that satisfy this equation. For centuries,
mathematicians have studied different Diophantine equations; besides
Fermat’s last theorem, some famous ones include Pell’s equation, and the
Erdos-Strauss conjecture. For more information on this intriguing branch of
mathematics, you may find the Wikipedia article of interest.
We are not certain that McDonald’s knows about Diophantine equations
(actually we doubt that they do), but they use them! McDonald’s sells
Chicken McNuggets in packages of 6, 9 or 20 McNuggets. Thus, it is
possible, for example, to buy exactly 15 McNuggets (with one package of 6
and a second package of 9), but it is not possible to buy exactly 16
nuggets, since no non-negative integer combination of 6’s, 9’s and 20’s
adds up to 16. 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.
Problem 1.
Show that it is possible to buy exactly 50, 51, 52, 53, 54, and 55
McNuggets, by finding solutions to the Diophantine equation. You can solve
this in your head, using paper and pencil, or writing a program. However
you chose to solve this problem, list the combinations of 6, 9 and 20 packs
of McNuggets you need to buy in order to get each of the exact amounts.
Given that it is possible to buy sets of 50, 51, 52, 53, 54 or 55 McNuggets
by combinations of 6, 9 and 20 packs, show that it is possible to buy 56,
57,…, 65 McNuggets. In other words, show how, given solutions for 50-55,
one can derive solutions for 56-65.
Theorem: If it is possible to buy x, x+1,…, x+5 sets of McNuggets, for some
x, then it is possible to buy any number of McNuggets >= x, given that
McNuggets come in 6, 9 and 20 packs.

Here is my code:
 for a in range(1,10):
for b in range(1,5):
for c in range(1,5):
mc=(6*a)+(9*b)+(20*c)
if mc==50:
print a,b,c
else:
print a,b,c
a=+1
b=b+1
c=c+1
and this is the output:
1 1 1
1 2 2
1 3 3
1 4 4
1 2 1
1 3 2
1 4 3
1 5 4
1 3 1
1 4 2
1 5 3
1 6 4
1 4 1
1 5 2
1 6 3
1 7 4
2 1 1
1 2 2
1 3 3
1 4 4
1 2 1
1 3 2
1 4 3
1 5 4
1 3 1
1 4 2
1 5 3
1 6 4
1 4 1
1 5 2
1 6 3
1 7 4
3 1 1
1 2 2
1 3 3
1 4 4
1 2 1
1 3 2
1 4 3
1 5 4
1 3 1
1 4 2
1 5 3
1 6 4
1 4 1
1 5 2
1 6 3
1 7 4
4 1 1
1 2 2
1 3 3
1 4 4
1 2 1
1 3 2
1 4 3
1 5 4
1 3 1
1 4 2
1 5 3
1 6 4
1 4 1
1 5 2
1 6 3
1 7 4
5 1 1
1 2 2
1 3 3
1 4 4
1 2 1
1 3 2
1 4 3
1 5 4
1 3 1
1 4 2
1 5 3
1 6 4
1 4 1
1 5 2
1 6 3
1 7 4
6 1 1
1 2 2
1 3 3
1 4 4
1 2 1
1 3 2
1 4 3
1 5 4
1 3 1
1 4 2
1 5 3
1 6 4
1 4 1
1 5 2
1 6 3
1 7 4
7 1 1
1 2 2
1 3 3
1 4 4
1 2 1
1 3 2
1 4 3
1 5 4
1 3 1
1 4 2
1 5 3
1 6 4
1 4 1
1 5 2
1 6 3
1 7 4
8 1 1
1 2 2
1 3 3
1 4 4
1 2 1
1 3 2
1 4 3
1 5 4
1 3 1
1 4 2
1 5 3
1 6 4
1 4 1
1 5 2
1 6 3
1 7 4
9 1 1
1 2 2
1 3 3
1 4 4
1 2 1
1 3 2
1 4 3
1 5 4
1 3 1
1 4 2
1 5 3
1 6 4
1 4 1
1 5 2
1 6 3
1 7 4.

Can someone please tell me whats wrong with my code?. I'm using for loops
to give possible valutes to a,b and c and then using the equation
(6*a)+(9*b)+(20*c) to determine possible values for a,b and c for
satisfying the equation.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem

2016-08-29 Thread Shahan Khan
Thankyou so much for taking the time it really means alot. I'll change the code 
and try again. I have just one question what purpose does " {} " serve here?

Sent from my iPhone

> On 29-Aug-2016, at 2:30 AM, zakaria  wrote:
> 
> if you print the values of a, b ,c that satisfy and don't satisfy the
> condiction cm == 50 at the same time, you can't know what works and
> what did not work.
> 
> here is the code that i wrote and worked well
> 
> for a in range(1, 11): # i replaced 10 by 11 to include the 10 
> for b in range(1, 6):  # same as before put 6 to include 5
> for c in range(1, 6):
> mc = (6*a) + (9*b) + (20*a)
> if mc == 50:
> print(a, b, c)
> 
> notice that i am using python 3.5, range is a generator like xrange in
> python 2
> 
> and the result is 2 for a and b and 1 for c
> I wish this coold help you
> 
> for what you said, here is the hard coded solution:
> 
> for a in range(1, 11):
>for b in range(1, 6):
>  for c in range(1, 6):
>mc = 6*a + 9*b + 20*c
>if mc == 50:
>  print('for 50 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b,
> c))
>if mc == 51:
>  print('for 51 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b,
> c))
>if mc == 52:
>  print('for 52 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b,
> c))
>if mc == 53:
>  print('for 53 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b,
> c))
>if mc == 54:
>  print('for 54 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b,
> c))
>if mc == 55:
>  print('for 55 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b,
> c))
> and the result was 
> 
> for 55 McNuggets, "a"= 1, "b"=1, "c"=2
> for 53 McNuggets, "a"= 1, "b"=3, "c"=1
> for 50 McNuggets, "a"= 2, "b"=2, "c"=1
> for 53 McNuggets, "a"= 4, "b"=1, "c"=1
> 
> 
> Or you can write a more elegant one:
> 
> for a in range(1, 11):
>for b in range(1, 6):
>  for c in range(1, 6):
>mc = 6*a + 9*b + 20*c
>if mc in range(50, 56):
>  print('for {} McNuggets, "a"= {}, "b"={}, "c"={}'.format(mc,
> a, b, c))
> 
> i used range(50, 56) to include the value 56
> 
> know , i wanted to know the purpose of this code:
>> a=+1
>> b=b+1
>> c=c+1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem

2016-08-29 Thread shahan khan
I changed the code a bit and this is the result:
for a in range(1,11):
for b in range(1,6):
for c in range(1,6):
mc=(6*a)+(9*b)+(20*c)
if mc==50:
print 'For 50 McNuggets:''a=',a,'b=',b,'c=',c
if mc==51:
print 'Fpr 51 McNuggets:''a=',a,'b=',b,'c=',c
if mc==52:
print 'For 52 McNuggets:''a=',a,'b=',b,'c=',c
if mc==53:
print 'For 53 McNuggets:''a=',a,'b=',b,'c=',c
if mc==54:
print 'For 54 McNuggets:''a=',a,'b=',b,'c=',c
if mc==55:
print 'For 55 McNuggets:''a=',a,'b=',b,'c=',c
Result:
For 55 McNuggets:a= 1 b= 1 c= 2
For 53 McNuggets:a= 1 b= 3 c= 1
For 50 McNuggets:a= 2 b= 2 c= 1
For 53 McNuggets:a= 4 b= 1 c= 1

Two questions:
1) why is it printing backwards? 2) why is it skipping 51,52 and 54?

On Mon, Aug 29, 2016 at 3:21 AM, Shahan Khan  wrote:

> Thankyou so much for taking the time it really means alot. I'll change the
> code and try again. I have just one question what purpose does " {} " serve
> here?
>
> Sent from my iPhone
>
> > On 29-Aug-2016, at 2:30 AM, zakaria  wrote:
> >
> > if you print the values of a, b ,c that satisfy and don't satisfy the
> > condiction cm == 50 at the same time, you can't know what works and
> > what did not work.
> >
> > here is the code that i wrote and worked well
> >
> > for a in range(1, 11): # i replaced 10 by 11 to include the 10
> > for b in range(1, 6):  # same as before put 6 to include 5
> > for c in range(1, 6):
> > mc = (6*a) + (9*b) + (20*a)
> > if mc == 50:
> > print(a, b, c)
> >
> > notice that i am using python 3.5, range is a generator like xrange in
> > python 2
> >
> > and the result is 2 for a and b and 1 for c
> > I wish this coold help you
> >
> > for what you said, here is the hard coded solution:
> >
> > for a in range(1, 11):
> >for b in range(1, 6):
> >  for c in range(1, 6):
> >mc = 6*a + 9*b + 20*c
> >if mc == 50:
> >  print('for 50 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b,
> > c))
> >if mc == 51:
> >  print('for 51 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b,
> > c))
> >if mc == 52:
> >  print('for 52 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b,
> > c))
> >if mc == 53:
> >  print('for 53 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b,
> > c))
> >if mc == 54:
> >  print('for 54 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b,
> > c))
> >if mc == 55:
> >  print('for 55 McNuggets, "a"= {}, "b"={}, "c"={}'.format(a, b,
> > c))
> > and the result was
> >
> > for 55 McNuggets, "a"= 1, "b"=1, "c"=2
> > for 53 McNuggets, "a"= 1, "b"=3, "c"=1
> > for 50 McNuggets, "a"= 2, "b"=2, "c"=1
> > for 53 McNuggets, "a"= 4, "b"=1, "c"=1
> >
> >
> > Or you can write a more elegant one:
> >
> > for a in range(1, 11):
> >for b in range(1, 6):
> >  for c in range(1, 6):
> >mc = 6*a + 9*b + 20*c
> >if mc in range(50, 56):
> >  print('for {} McNuggets, "a"= {}, "b"={}, "c"={}'.format(mc,
> > a, b, c))
> >
> > i used range(50, 56) to include the value 56
> >
> > know , i wanted to know the purpose of this code:
> >> a=+1
> >> b=b+1
> >> c=c+1
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem

2016-08-29 Thread shahan khan
Yes i format my code but i can't figure out this new problem

On Mon, Aug 29, 2016 at 3:20 AM, Joel Goldstick 
wrote:

> On Sun, Aug 28, 2016 at 10:46 AM, shahan khan 
> wrote:
> > Hello
> > I'm teching myself Python using MIT opencourse ware. I'm a beginner and
> > have some what knowledge of c and c++. I'm using Python version
> >
> > Here is the problem:
> > McDiophantine: Selling McNuggets
> > In mathematics, a Diophantine equation (named for Diophantus of
> Alexandria,
> > a third century Greek mathematician) is a polynomial equation where the
> > variables can only take on integer values. Although you may not realize
> it,
> > you have seen Diophantine equations before: one of the most famous
> > Diophantine equations is:
> > xn + yn= zn.
> > For n=2, there are infinitely many solutions (values for x, y and z)
> called
> > the Pythagorean triples, e.g. 32 + 42 = 52. For larger values of n,
> > Fermat’s famous “last theorem” states that there do not exist any
> positive
> > integer solutions for x, y and z that satisfy this equation. For
> centuries,
> > mathematicians have studied different Diophantine equations; besides
> > Fermat’s last theorem, some famous ones include Pell’s equation, and the
> > Erdos-Strauss conjecture. For more information on this intriguing branch
> of
> > mathematics, you may find the Wikipedia article of interest.
> > We are not certain that McDonald’s knows about Diophantine equations
> > (actually we doubt that they do), but they use them! McDonald’s sells
> > Chicken McNuggets in packages of 6, 9 or 20 McNuggets. Thus, it is
> > possible, for example, to buy exactly 15 McNuggets (with one package of 6
> > and a second package of 9), but it is not possible to buy exactly 16
> > nuggets, since no non-negative integer combination of 6’s, 9’s and 20’s
> > adds up to 16. 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.
> > Problem 1.
> > Show that it is possible to buy exactly 50, 51, 52, 53, 54, and 55
> > McNuggets, by finding solutions to the Diophantine equation. You can
> solve
> > this in your head, using paper and pencil, or writing a program. However
> > you chose to solve this problem, list the combinations of 6, 9 and 20
> packs
> > of McNuggets you need to buy in order to get each of the exact amounts.
> > Given that it is possible to buy sets of 50, 51, 52, 53, 54 or 55
> McNuggets
> > by combinations of 6, 9 and 20 packs, show that it is possible to buy 56,
> > 57,…, 65 McNuggets. In other words, show how, given solutions for 50-55,
> > one can derive solutions for 56-65.
> > Theorem: If it is possible to buy x, x+1,…, x+5 sets of McNuggets, for
> some
> > x, then it is possible to buy any number of McNuggets >= x, given that
> > McNuggets come in 6, 9 and 20 packs.
> >
> > Here is my code:
> >  for a in range(1,10):
> > for b in range(1,5):
> > for c in range(1,5):
> > mc=(6*a)+(9*b)+(20*c)
> > if mc==50:
> > print a,b,c
> > else:
> > print a,b,c
> > a=+1
> > b=b+1
> > c=c+1
>
> Welcome to the list.
>
> You need to format your code correctly for anyone to help you.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem

2016-08-29 Thread Shahan Khan
I understand your argument. You're saying M(50),M(51)and M(52) is basically a 
set of 6 9 and 20 packs and we should approach it by using lower values 
starting from 0 for one or two variables to simply the solution on paper. I 
think I have some idea now as to how to approach this problem.

Sent from my iPhone

> On 29-Aug-2016, at 5:18 AM, Danny Yoo  wrote:
> 
>> On Sun, Aug 28, 2016 at 7:46 AM, shahan khan  wrote:
>> Hello
>> I'm teching myself Python using MIT opencourse ware. I'm a beginner and
>> have some what knowledge of c and c++. I'm using Python version
> 
> 
> 
>> Here is my code:
> [code cut]
> 
> Before showing code, try to express what you're trying to do in
> English first.  That is, your explanation near the bottom:
> 
>> I'm using for loops to give possible valutes to a,b and c and then using the 
>> equation (6*a)+(9*b)+(20*c) to determine possible values for a,b and c for
> satisfying the equation.
> 
> ... this should actually be presented because it's the most important
> part!  The problem with code is that code just executes. Any
> expectations we have about what that code does is a matter of human
> interpretation.
> 
> 
> 
> Anyway, from looking at your original code (modulo indentation), I
> *think* the treatment as a search problem, looking for the values of
> the parameters a, b, c is reasonable, with the way you're doing nested
> for loops,
> 
>> for a in range(1,10):
>> for b in range(1,5):
>> for c in range(1,5):
> 
> However, I am not entirely convinced that the search bounds are
> actually sound.  Here's why: I don't know why the numbers 1, 10, 5,
> and 5 were chosen in the loop: they seem arbitrary, and you need to
> say where those numbers came from.  I suspect there's a problem with
> them anyway.
> 
> 
> One concrete reason why I don't think it works: change the problem
> slightly.  Say that we're trying to find a solution for the parameters
> where the Diophantine equation evaluates to 6.  Basically, the really
> simple edge case.
> 
> What change would you make to your code for this simpler case?  I'd
> assume that the simplest thing to do would be to change the target
> value in the test expression, from:
> 
>if mc==50: ...
> 
> to
> 
>if mc == 6: ...
> 
> 
> Now, before we even try your program, we *know* there's a trivial
> solution for a, b, and c such that the equation sums to six!
> 
>a = 1
>b = 0
>c = 0
> 
> However, looking back at program structure, we can see, even without
> looking at the rest of the code, that it can't possibly consider such
> parameters, because it's assuming that all three variables have to be
> positive.
> 
> So that's something that we know needs to be fixed in some way.
> 
> 
> 
> My take on the problem: this sounds very much like a question begging
> for a dynamic programming approach, rather than a nested loop
> approach.  The reason I say this is because of the part of the problem
> statement:
> 
> ###
> Theorem: If it is possible to buy x, x+1,…, x+5 sets of McNuggets, for some
> x, then it is possible to buy any number of McNuggets >= x, given that
> McNuggets come in 6, 9 and 20 packs.
> ###
> 
> would be the beginning of a mathematical induction proof, which is the
> scent of a dynamic programming problem.
> 
> 
> I'd sketch this as follows: let's call M(n) a boolean function on the
> natural numbers that's true if we can buy n McNuggets by *any*
> combination of 6, 9, and 20 packs.
> 
> We know that:
> 
>* M(6) is true
>* M(9) is true
>* M(20) is true
> 
> Why?  Because we can take a single 6-pack, or a single 9-pack, or a
> single 20-pack, and each of these is a way to buy 6, 9, or 20
> McNuggets.
> 
> From that core collection of knowledge, that inductive base, we can
> start investigating what else we can discover by building upon that
> knowledge inductively.
> 
> 
> Example: we know that since M(6) is true, that M(12) is true, because
> we can include another 6-pack to the one that built up M(6).
> Furthermore, we know that M(15) is true, because M(6) is true, and we
> can include another 9-pack to the one that built up M(6).
> 
> 
> What about M(50)?  Well, not quite sure.  However, I do know this: if
> M(50) is true, then *at least one* of the following has to be true:
> 
>   a.  M(44) is true
>   b.  M(41) is true
>   c.  M(30) is true
> 
> How in the wo