Traversing Switch Configurations

Peggy is a treasure hunter trying to open two treasure boxes. One box has 2 switches and the other has 4 switches. All of the switches are initially turned off. To open a treasure box,

  • she must only change one switch at a time;
  • she needs to register every possible combination exactly once.

Peggy successfully opens the box with 2 switches in the following manner:

The sequence in which Peggy toggles the switches The sequence in which Peggy toggles the switches

Can Peggy open the box with 4 switches?


Hint: What if Peggy's box had 3 switches instead?

No Yes

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

10 solutions

Arjen Vreugdenhil
Aug 13, 2017

Number the dials arbitrarily from 0 0 to 3 3 . On the n n th turn ( n = 1 , 15 n = 1, \dots 15 ), turn dial m m , where 2 m 2^m is the greatest power of two that divides n n . In other words, turn dials in this order: n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 m 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 \begin{array}{r|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c} n & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 \\ \hline m & 0 & 1 & 0 & 2 & 0 & 1 & 0 & 3 & 0 & 1 & 0 & 2 & 0 & 1 & 0 \end{array}


This problem is equivalent to the search for a Gray code : a list of all 2 n 2^n different combinations of n n bits, where each entry in the list differs from its neighbors by exactly one bit. The key fact is that Gray codes exist for all n 1 n \geq 1 .

The search for a n n -bit Gray code is equivalent to finding a Hamiltonian path (or loop) through a n n -dimensional cube.

To generate a Gray code, start with A 0 = A_0 = an arbitrary combination of n n bits. To generate A n A_n from A n 1 A_{n-1} , change the m m th bit, where 2 m 2^m is the greatest power of two dividing n n .

Thus,

n A n 0 0000 1 0001 change bit 0 2 0011 change bit 1 3 0010 change bit 0 4 0110 change bit 2 5 0111 change bit 0 6 0101 etc. 7 0100 8 1100 9 1101 10 1111 11 1110 12 1010 13 1011 14 1001 15 1000 \begin{array}{r|c|l} n & A_n \\ \hline 0 & 0000 \\ 1 & 000\mathbf 1 & \text{change bit 0} \\ 2 & 00\mathbf 11 & \text{change bit 1} \\ 3 & 001\mathbf 0 & \text{change bit 0} \\ 4 & 0\mathbf 110 & \text{change bit 2} \\ 5 & 011\mathbf 1 & \text{change bit 0} \\ 6 & 01\mathbf 01 & \text{etc.} \\ 7 & 010\mathbf 0 \\ 8 & \mathbf 1100 \\ 9 & 110\mathbf 1 \\ 10 & 11\mathbf 11 \\ 11 & 111\mathbf 0 \\ 12 & 1\mathbf 010 \\ 13 & 101\mathbf 1 \\ 14 & 10\mathbf 01 \\ 15 & 100\mathbf 0 \end{array}

Moderator note:

Grey codes are useful in digital communications. For example:

Suppose someone is sending a series of numbers representing positions on a line segment. If the numbers are coded by a Grey Code sequence with 2-bit numbers representing positions from left to right (as opposed to just using the sequence 1, 2, 3, 4 converted into binary) then any 1-bit error will be spatially adjacent to the correct position. This makes it easier to create an error correction system.

The 4-bit version is used in 2-dimensional applications in a similar way.

Thank you for the information on Gray codes! I learned something new.

Mark Lama - 3 years, 10 months ago

To generate A n A_n from A n 1 A_{n-1} , change the m m th bit, where 2 m 2^m is the greatest power of two dividing n n .

I think it'd be helpful if you illustrated this with an example

Agnishom Chattopadhyay - 3 years, 10 months ago

Log in to reply

I will add a column to the table.

Arjen Vreugdenhil - 3 years, 10 months ago

@ Challenge Master note: I have to disagree with the last statement that "any 1-bit error will be spatially adjacent to the correct position". For instance, in the example table I gave, 0101 0101 corresponds to position 6, but 1101 1101 (which is only one bit off) corresponds to position 9, and 0001 0001 (also one bit off) corresponds to position 1.

Arjen Vreugdenhil - 3 years, 9 months ago

Log in to reply

@Arjen Sorry, the 4-bit version is 2-D (which tends to be how it is often used). I was trying to explain a simpler version in 1-D which would be just four numbers -- I've tweaked the text to reflect the difference.

Jason Dyer Staff - 3 years, 9 months ago

I am a secondary school student . Can you explain me

Janesh G - 3 years, 7 months ago

What are grey codes

Janesh G - 3 years, 7 months ago

Please have someone who does not know the answer READ the question. Both the question and the illustration refer to an end state - or at least that is the way it reads. Your answer refers to 2^n combination of n bits for setting the combs. Nowhere in the problem statement (at face value) is there any information about order of of setting the switches or requirements for the full application of adjacent switch differences. The main reason people who know math miss word problems is that they answer what was written and not what is meant.

David Brown - 3 years, 7 months ago

Log in to reply

The problem is not merely talking about "an end state", because it says that Peggy "needs to register every possible combination exactly once". I don't see a discrepancy between what was written and what was (supposedly) meant.

I am one of the people who read the problem without knowing the answer. Only after carefully reading the "rules" of the game, I realized it tied in with math familiar to me--Grey codes, in this case.

Arjen Vreugdenhil - 3 years, 7 months ago
Tung Phung
Aug 13, 2017

Relevant wiki: Induction - Combinatorics Applications

Suppose it is possible for n switches, we will prove it is also possible for n + 1 switches.

For n+1 switches:

  • Step 1: set the first switch to Off

  • Step 2: for the remaining n switches, follow the exact sequence of combinations used in the solution for n switches.

  • Step 3: turn the first switch to On

  • Step 4: for the remaining n switches, follow in reverse order the exact sequence of combinations that were used in Step 2.

Neat observation, indeed.

Agnishom Chattopadhyay - 3 years, 10 months ago

Noticing that pattern was how I solved it, too! Since for n = 1 n=1 the sequence is trivially easy to find, solutions for increasing n n can be quickly built up. It would be easier if you edited steps 2 and 4 to read something like: "Step 2: for the remaining n switches, follow the exact sequence of combinations used in the solution for n switches. Step 4: for the remaining n switches, follow in reverse order the exact sequence of combinations that were used in Step 2.

Mark Lama - 3 years, 10 months ago

Log in to reply

Thanks for your comment, I edited my solution!

Tung Phung - 3 years, 10 months ago

To beginning mathematicians out there who don't have any idea as to what Arjen Vreugdenhil is saying (it is a great solution I admit), this will be one of the possible solutions.

"F" would be Off and "T" would be On

  1. F-F-F-F
  2. F-F-F-T
  3. F-F-T-T
  4. F-F-T-F

Notice the pattern. 2^2 possible permutations of the last two switches. Notice that we didn't change the 1st and 2nd switches. We are only finding for the permutations for the two switches.

  1. F-T-T-F
  2. F-T-F-F
  3. F-T-F-T
  4. F-T-T-T

2^3=8 permutations for all the last three switches. Why? Because We changed the 2nd Switch and then again we find the permutations of the last two switches.

  1. T-T-T-T
  2. T-T-T-F
  3. T-T-F-F
  4. T-T-F-T
  5. T-F-F-T
  6. T-F-T-T
  7. T-F-T-F
  8. T-F-F-F

Then, repeating the iteration, now with the first switched on, we get another 8 permuations. Why? Because we are doing what we did in steps one to eight, with one exception, the first switch is ON.

To generalize, we need 2^n individual steps to find all the possible permutations without repeating one. Here, n=4, so we need 2^n = 2^4 = 16 steps. The base 2 is from the fact that there is two possible choices for a single switch, that are, ON and OFF.

Forgive me as this is not a proof. Tung Phung has already given one. I am trying to let beginner math students to understand.

Every switch has two choices on or off since these events happen together we can apply multiplication rule of combinatorics to get 2✖️2✖️2✖️.... n times to get 2^n

Jivesh Kesar - 3 years, 9 months ago
David Fairer
Aug 15, 2017

The hint was 'what if the box had 3 switches'. Well if it had 3 switches, then Peggy should do what she had done for the 2 switches. Then turn the 3rd switch. Then do exactly the reverse of the turns for the 2 switches. And now seeing as it has 4 switches, and we know that all combinations could be achieved for 3 switches, just turn the 4th switch. And then do the reverse of the steps that we taken for 3 switches. | In general if there are n switches, and you have turned the switches for (n - 1) switches, turn the nth switch. And then turn in reverse for the (n - 1) switches. Regards, David

John T Reagan
Aug 15, 2017

The question could be re-worded: Can Peggy run through every possible combination of switch settings exactly once? Of course she can! How long that takes her and how well she can keep track of what she is doing are both practical issues, but certainly there is nothing preventing her from running through all the combinations to get the box open.

Sunil Nandella
Aug 18, 2017

although the problem is unique in making us understand combinatorics, whats the problem exactly?

Peter Moczo
Aug 18, 2017

To put the specific solution into simple terms Peggy could simply iterate what she did for the first box (with 2 switches) on the top 2 switches of the second box for each similar step of the lower 2 switches. Thus taking 4 steps x 4 steps both being the same sequence she used for the first box.

Wai Yee Low
Aug 16, 2017

Let 0 denotes switch off and 1 denotes switch on. There are 2 4 ^{4} combinations of these 0 and 1. Let S be the set that denotes all possible sample points (i.e. S is the sample space). S = {{0,0,0,0},{1,0,0,0},{ 0,1,0,0},{1,1,0,0}, {0,0,1,0},{1,0,1,0},{0,1,1,0},{1,1,1,0},{0,0,0,1},{1,0,0,1},{0,1,0,1},{1,1,0,1},{0,0,1,1},{1,0,1,1},{0,1,1,1},{1,1,1,1}}.

The picture shows that the starting position is {0,0,0,0}. We need to show that there is a sequence with {0,0,0,0} as the starting point and it covers all 16 possible elements of S exactly once. Verify that one possible sequence is {0,0,0,0} - {1,0,0,0} - {1,1,0,0} - { 0,1,0,0} - {0,1,1,0} - {0,0,1,0} - {1,0,1,0} - {1,1,1,0} - {1,1,1,1} - {0,1,1,1} - {0,0,1,1} - {1,0,1,1} - {1,0,0,1} - {1,1,0,1} - {0,1,0,1} - {0,0,0,1}

This problem can be solved using {0,0,0,0} as the starting point and use a computer program to write all possible sequences that cover all 16 elements. Then do math to find absolute difference of the next sequence with the previous sequence. For example, absolute|{1,0,0,0} minus {0,0,0,0}| will give one as the answer. As long as there is a sequence that always give one for the 15 absolute differences calculation, we have an answer. I have not computed how many answers are possible but it's not hard to write an algorithm to do so.

I just want to add to Arjen that this is not the only possible combination. There is many ways of changing one bit per step, i.e.: 0000, 0001, 0011, 0010, 0110, 0100, 0101, 0111, 1111, 1110, 1100, 1101, 1001, 1000, 1010, 1011, as another possibility.

John Allums
Aug 15, 2017

0000
1000
1100
0100
0110
0010
0011
0001
0101
1101
1001
1011
1010
1110
1111
0111










0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...