views:

467

answers:

14

I've never been much for math and I'm hoping that someone can help me out with the following.

I have 5 boxes:

 1   2   3   4   5
[ ] [ ] [ ] [ ] [ ]

The boxes can either be white, gray, or black (or think of it as 0, 1, 2)

How many possible states can the box set be in?

What is the pseudocode (or in any language) to generate all the possible outcomes??

ie...

00000
00001
00011
00111

etc, etc...

I really appreciate any help anyone can give me with this.

A: 

The number of possibilities is 3 to the power 5

If you loop from 0 to that number minus 1 and express it in base 3 you will have all the possibilities (remember to prepend 0s where necessary)

In Ruby:

number_of_possibilities = 3**5-1

for i in (0..number_of_possibilities)
  base_3_number = i.to_s(3)
  puts "%05d" % base_3_number # number formatting used to prepend 0s where necessary
end
DanSingerman
A: 

This seems like a homework problem. I'll just give you some help as to the solution then.

What you are saying is that each box has three states, which are all independent. One box would have 3 solutions, and two boxes would have 3 * 3 solutions - for each state of the first box the second box would have three states as well. Extend that to 5 boxes.

To generate each solution, you can just cycle through it. It is easy to make nested for loops for each box, and multiplying by powers of 10 can let you show the number at once.

You can generalize the code for multiple boxes in a similar way.

Andrei Krotkov
Aren't you giving him the full solution here?
Kena
If it's homework, is a full solution unhelpful?
ChrisW
Who cares? Let him pass the course and fail at life.
Geoffrey Chetwood
A: 

the number of states is 3^5.

pseudocode is

for value from 0 to 3^5-1
    print base3(value)

where base3 is a function that repeatedly takes modulo 3 to get a digit, then removes that digit (by dividing by 3)

Jimmy
+5  A: 

To answer your first question, what would the answer be if the boxes could contain only one of two values? So, what's the answer if the boxes contain one of three values?

To answer your second question, what pseudocode generates all possible outcomes of one box? Now, pseudocode generates all possible outcomes of two boxes?

ChrisW
+3  A: 

I'd recommend solving the problem on paper first. Try to solve it with a smaller number of boxes (maybe three), and list all possibilities. Then, think of how your reasoning went, or how you'd explain what you did to a small child.

Kena
A: 

Hint: imagine that each box is a position in a number and each colour is a different digit. In the real world, how many combinations (including zero) do you get with 2 positions and 10 possible digits? What about 3 positions? What's the relationship between adding an extra position and the number of combinations, given the number of digits you have available?

Kylotan
+6  A: 

the answer for the number of combinations is: 3x3x3x3x3 (3^5) since each box can have 3 possible colors.

As for generating the outcomes, see if you can figure it out using this matrix with 0, 1, or 2 to represent the color of the box. On a smaller scale (lets assume 3 boxes) it would look like this:

0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
1 0 0
1 0 1
1 0 2
1 1 0
1 1 1
1 1 2
1 2 0
1 2 1
1 2 2
2 0 0
2 0 1
2 0 2
2 1 0
2 1 1
2 1 2
2 2 0
2 2 1
2 2 2
yx
A: 

Unique number of combinations: 3^5=243

Code:

n = 0
for i = 0 to 3^5-1
{
    s = ""
    for j = 1 to 5
    {
        d = n mod 3
        s = toascii(d) . s
        n = n / 3
    }
    println s
    i = i + 1
}
edgar.holleis
-1 because writing homework for other people is evil
Kena
+5  A: 

This is a classic permutation generation problem. You have 3 possibilities for each position, and 5 positions. The total number of generated string is 3^5 = 243. You need recursion if you want a general solution (a simple iterative loop only works for a single instance of the problem).

Here's a quick example:

public static void Main(string[] args){

    Generate("", 5);
}

private void Generate(string s, int limit)
{
    if (s.Length == limit)
     Console.WriteLine(s);
    else
    {
     Generate(s+"0", limit);
     Generate(s+"1", limit);
     Generate(s+"2", limit);
    }
}
DavidN
Sweet! I never reach for recursion as my first solution but this is elegant.
DMKing
A: 

Here's how I first learned to do this: first think about how many choices you are making. You are making five choices, one for each box. So write down five blank lines with multiplication signs:

__ x __ x __ x __ x __ = ?

In each blank, write the number of objects you have to choose from for that box. Since you have 3 numbers to choose from for each box, you write a 3 in every blank:

3 x 3 x 3 x 3 x 3 = 243

This gives you the total number of permutations for those choices.

yjerem
A: 

Can I ask what about this you don't understand or whats tripping you up? I see that everyone here has simply answered the question, but if you've copied their answers, you've learned nothing, and thus completely missed the point of the homework. Assuming your next lesson builds upon this one, you're just going to fall further behind.

If you either worked for me or were in my class I'd simply ask the following...

"How do you think the problem should be solved?" The answer to which might reveal where you're getting hung up. A wise professor of mine at CMU once said "I can't help you understand this until you know what you don't understand" I never did figure out what I didn't understand and I dropped his class, but the lesson stuck with me.

I know its probably too late, but for these homework questions I really think we should be helping the person learn as opposed to simply providing an answer and doing their homework for them.

Jonathan Beerhalter
A: 

Don't even try to write code to answer this! The reason is that you need some very large numbers (factorials) to calculate it. These create numbers much larger than any base type in the CLR. You can use this opensource library to do the calculation.

Noel Kennedy
A: 

Your problem needs nothing more than the rule of product in combinatorics.

You can choose the state of the first box in 3 ways, and the state of the second box in 3 ways, and ... and the state of the 5th box in 3 ways. The number of ways in which you can set the state of all the boxes is the product of all the five (equal) numbers of ways, i.e. 3x3x3x3x3 = 35.

Similar question: how many numbers can you form with 5 digits in the decimal system, counting the leading zeros? That is, how many numbers are there from 00000 to 99999? You can choose the first digit in 10 ways (0...9), and so on and so on, and the answer is 10x10x10x10x10 = 100000, as you already know.

Federico Ramponi
+1  A: 

Thank you all for your answers, at least those of you who actually gave me one.

While I can appreciate that the question sounded like it was pulled straight out of Computer Science 101, it wasn't. The irony of the matter is that it was for real life on a real deadline and I didn't have time to hearken back to when I was being taught this stuff and said to myself, "when am I ever going to need this crap"

If I wanted to be patronized and treated like a school boy I would go back to my elementary school and ask my 5th grade teacher if I can go to the bathroom

Thanks again

Michael