A Riddle with 22 Legs

My kid brought home some math homework tonight. Here's the assignment:
A Riddle with 22 Legs
I counted 22 legs in my house.
All the legs were on cats, people, and spiders.

How many of each creature -- cats, people, and
spiders -- might be in the house?

See how many different ways you can
answer this riddle.

There are many possible answers.
How many can you find?

How do you know you have all
the possible answers?


So I start thinking to myself, okay a spider has 8 legs, a cat has 4 legs, and a person has 2 legs. I divide 22 by 8, by 4, and by 2 and find that I can't have more than 2 spiders, I can't have more than 5 cats, and I can't have more than 11 people.

So, I explain all this to my kid and explain to take the number of spiders and multiply that by 8, then the number of cats and multiply that by 4, and the number of people and multiply that by 2 to figure out the number of legs. I also explain that you'll never have more than 2 spiders, never more than 5 cats, and never more than 11 people, so find all the combinations within that range.

After about an hour of my kid finding the same combinations over and over. I think, maybe I can do this in an Excel spreadsheet but then I'm having the same problem as my kid, I can't keep track of all my answers. So I get thinking, this is why we invented computers, so we don't have to figure out mundane crap like this. So, here are the answers and the code:


Answers:












You cannot have more than 12 possible combinations (as long as you're using positive integers) because you can never have more than 11 people; (0,1,2,3,4,5,6,7,8,9,10,11)
there are 12 numbers in that sequence.

There is no way in heck I would have done that problem without writing code.
Why aren't they teaching my kid Java yet?!?

public class riddle22 {
public static void main(String [] args) {
int x;
int y;
int z;
int total;

for(x = 0;x < 3;x++){
for(y = 0;y < 6;y++){
for(z = 0;z < 12;z++){
total = (x * 8)+(y * 4)+(z * 2);
if (total == 22)
System.out.println
(x+" + "+y+" + "+z+" = "+total);
}
}
}
}
}

No comments: