16

I am learning Mathematica because I love it. I also love solving puzzles so I think it would be a nice way to learn Mathematica through puzzles. This is first puzzle in series I intend to solve.

So here is the problem.

  • People = 20
  • Food = 20
  • Restrictions;

    • A man eats 2 units
    • A woman consumes 1.5 units
    • A baby can take only .5 units of food
    • Combined number of men women and babies must equal 20
    • All units of food must be consumed.

There are four possible solutions, I would like to learn

  1. shortest syntax to get a random solution

  2. syntax/program which gives all possible solution

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Jawad_Mansoor
  • 321
  • 1
  • 9
  • @m_goldberg I disagree with change of tag as it is an iteration problem. i.e. it has less number of equations to solve than the variables, so no matrix solution is possible for them. Only way to solve such problem is through iteration. So iteration tag was best suited here. – Jawad_Mansoor Feb 25 '17 at 17:08
  • No iteration is needed to solve this problem in Mathematica. – m_goldberg Feb 25 '17 at 17:09
  • Alright then, if you think you can do that then do it on paper.

    You will find getting solutions to questions which have less number of equations than variables (which make the equations) is not easy. Because you will have to hit and try to get correct answer

    Anyway, if you REALLY KNOW that there is a way to solve such kind of puzzles/questions then do tell me I am always eager to learn.

    – Jawad_Mansoor Feb 25 '17 at 17:13
  • 3
    You already got an answer from mikado that doesn't use iteration. – m_goldberg Feb 25 '17 at 17:16
  • @m_goldberg if that was not iteration then I don't know what is. – Jawad_Mansoor Feb 25 '17 at 17:27
  • @Jawad_Mansoor That was, in fact, not an iterative procedure as I understand it. An iteration process generates a sequence of improving approximate solutions for a problem. That is not what happens in the solutions proposed below. Those are all direct methods. – MarcoB Feb 26 '17 at 00:07
  • @MarcoB thank you for replying. Okay, but I thought that was happening in the answers. I mean, it will evaluate one answer if it is in correct then it will go to find another answer until it finds the correct answer.

    So, what is direct method?

    – Jawad_Mansoor Feb 26 '17 at 08:12
  • @m_goldberg thank you for improving question – Jawad_Mansoor Feb 26 '17 at 08:14
  • Both of the replying members, Goldberg and Macro, can you put some links here which can differentiate between iterative and direct questions and answers so that my understanding of them becomes better. Thank you – Jawad_Mansoor Feb 26 '17 at 08:16

5 Answers5

21

Just write the problem literally and use Reduce

Reduce[
 m >= 0 && w >= 0 && b >= 0 && {m, w, b} ∈ Integers && 
  2 m + 3/2 w + 1/2 b == 20 && m + w + b == 20, {m, w, b}]

(* (m == 0 && w == 10 && b == 10) || (m == 2 && w == 7 && b == 11) || 
   (m == 4 && w == 4 && b == 12)  || (m == 6 && w == 1 && b == 13) *)
MarcoB
  • 67,153
  • 18
  • 91
  • 189
mikado
  • 16,741
  • 2
  • 20
  • 54
  • man that was fast, and thank you. :) Really appreciate that. – Jawad_Mansoor Feb 25 '17 at 16:48
  • I found these also work

    'Reduce[2 m + 3/2 w + 1/2 b == 20 && {m, w, b} [Element] Integers && {m, w, b} >= 0 && m + w + b == 20]'

    And

    'Reduce[2 m + 3/2 + 1/2 b == 20 && w + m + b == 20 && {m, w, b} >= 0, {m, w, b}, Integers]'

    But since they are based on your code so kudos to you my friend. Also, I just copied the e 'MEMBER' I don't know how to write it. How to write e?

    – Jawad_Mansoor Feb 25 '17 at 17:00
  • I don't know how to format code in code format, as you did. Please tell me how to format that also. Thank you – Jawad_Mansoor Feb 25 '17 at 17:04
  • 1
    @Jawad to format code use backward apostrophes, the top left key of my keyboard, with the tilde (~). – Nicholas G Feb 25 '17 at 17:21
  • Reduce[2 m + 3/2 w + 1/2 b == 20 && {m, w, b} [Element] Integers && {m, w, b} >= 0 && m + w + b == 20]

    Reduce[2 m + 3/2 + 1/2 b == 20 && w + m + b == 20 && {m, w, b} >= 0, {m, w, b}, Integers]

    Thank @NicholasG man

    – Jawad_Mansoor Feb 25 '17 at 17:33
20

We can do this more efficiently using IntegerPartitions:

Counts /@ IntegerPartitions[20, {20}, {1, 3, 4}/2]
{
 <|2 -> 6, 3/2 -> 1, 1/2 -> 13|>,
 <|2 -> 4, 3/2 -> 4, 1/2 -> 12|>,
 <|2 -> 2, 3/2 -> 7, 1/2 -> 11|>,
 <|3/2 -> 10, 1/2 -> 10|>
}

Also as requested code for only one solution:

Counts /@ IntegerPartitions[20, {20}, {1, 3, 4}/2, 1]
{<|2 -> 6, 3/2 -> 1, 1/2 -> 13|>}

This does not generate all and then throw some away; it only generates the one requested.

As a rule when working any problem similar to this I try to apply IntegerPartitions as when it fits it is usually much faster than Solve, Reduce, etc. Some examples:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 1
    Nice. I didn't know we could use rationals in IntegerPartitions – Simon Woods Feb 25 '17 at 22:54
  • @Mr.Wizard thank you for introducing me to this option of IntegerPartitions. Useful for 'Nickel and Dime' problems...:) – ubpdqn Feb 26 '17 at 03:30
  • @mr.wizard thank you. I never imagined a community so prolific with genius minds and yet to humble and generous. Thank you all, everybody for participating.

    I know I need to learn a lot and this is my starting point and I see it is good for many other people as well they are learning too.

    – Jawad_Mansoor Feb 26 '17 at 07:55
  • @mr.wizard how do you make hyper links appear like that? They are neat and short. I mean I don't see a full length link like

    [Link] http://mathematica.stackexchange.com/questions/22397/how-do-i-generate-a-set-of-n-tuples-containing-integral-solutions-to-a-linear-eq#22400 [/Link]

    Instead of just a few words as you mentioned integerpartitions (off site link) and three on site links.

    – Jawad_Mansoor Feb 26 '17 at 11:43
  • @Jawad For the first kind I am using the Ref button added by halirutan's wonderful toolbar extension. The three links at the bottom are auto-formatted by the site software; simply include a bare URL like http://mathematica.stackexchange.com/q/79587/ and it appears as a link with a proper title. Start a line with - to create a "bullet point" as I used for those last three links. – Mr.Wizard Feb 26 '17 at 11:49
18

Another solution:

Select[FrobeniusSolve[{20, 15, 5}, 200], Total[#] == 20 &]

{{0, 10, 10}, {2, 7, 11}, {4, 4, 12}, {6, 1, 13}}

The first element in each list is the number of men, the second element is the number of women, and the third element is the number of babies.

C. E.
  • 70,533
  • 6
  • 140
  • 264
  • I did not understand that solution at all. However it is correct. But how does it work, please explain. – Jawad_Mansoor Feb 25 '17 at 17:29
  • 2
    @Jawad_Mansoor You have to read the documentation for FrobeniusSolve as that is the core of the solution. The Select comes from the fact that FrobeniusSolve returns all possible combinations of any number of men, women, and children. Select picks out just those solutions where the total number of people is 20. – C. E. Feb 25 '17 at 17:42
10

YAW: Yet Another Way.

FindInstance seems created for such tasks:

Let m = number of men, w = number of women, b = number of babies.

FindInstance[{2 m + (3/2) w + (1/2) b == 20, m + w + b == 20, m >= 0, w >= 0, b >= 0}, {m, w, b}, Integers, 10]

(*{{m -> 0, w -> 10, b -> 10}, {m -> 2, w -> 7, b -> 11}, {m -> 4,  w -> 4, b -> 12}, {m -> 6, w -> 1, b -> 13}}*)
bobbym
  • 2,628
  • 2
  • 15
  • 20
  • Okay, it seams to be working, but where does the 10 in the end come from? I appreciate help. Thank you – Jawad_Mansoor Feb 25 '17 at 18:12
  • 2
    That tells FindInstance to look for 10 solutions, else it will only look for 1 by default. Remember, the number of solutions was a given. I wanted to be sure that there were no others. – bobbym Feb 25 '17 at 18:14
8

Not all solutions, but the one that minimizes the number of babies to feed

LinearProgramming[{0, 0, 1}, {{2, 1.5, .5}, {1, 1, 1}}, {{20, 0}, {20, 0}}, 0, Integers]
swish
  • 7,881
  • 26
  • 48