Simulation Solution to a Salt-&-Pepper Puzzle

Gene Michael Stover

Monday, 5 January 2004

Copyright © 2004 by Gene Michael Stover. All rights reserved. Permission to copy, store, & view this document unmodified & in its entirety is granted.


Contents

1 Introduction

At lunch the other day, a coworker told the rest of us a logic puzzle. We had some good discussion about the solution, but because I'm a programmer, I wrote a program to simulate the answer.

2 The Puzzle

Here's the description of the puzzle.

2.1 Given

Here's the problem set-up.

  1. You have to containers.
  2. One is a salt shaker. The other is a pepper shaker.
  3. Their volumes are the same.
  4. The salt shaker is nearly full of salt. It has enough empty space to hold a pinch of something.
  5. The pepper shaker is nearly full of pepper. It contains exactly the same volume of pepper that the salt shaker contains of salt.
  6. Like the sale shaker, the pepper shaker could hold a pinch of something.
  7. In case it matters, you may assume that the size of a grain of salt is the same as the size of a fleck of pepper.

2.2 Action

Starting with the setup from the Given section, do these steps:

  1. Take a ``pinch'' of salt from the salt shaker.
  2. Put the pinch of salt in the pepper shaker.
  3. Thoroughly mix the contents of the pepper shaker.
  4. Take a pinch of pepper from the pepper shaker. Notice that this pinch will contain a mixture of salt & pepper.
  5. Put this salt-&-pepper pinch into the salt shaker.

In case it matters, for this problem, a ``pinch'' of something always has the same size. For example, if a pinch of salt has 100 grains of salt, then a pinch of pepper has 100 grains of pepper.

2.3 Question

After doing the steps in the Action section, the salt shaker will contain a certain volume of a salt-&-pepper mixture. The pepper shaker will contain a certain volume of another salt-&-pepper mixture. Each of those mixtures will have its own concentration of salt relative to pepper.

Answer this: Are the two volumes the same? Is the concentration of salt in the salt shaker's mixture the same as the concentration of pepper in the pepper shaker's mixture?

3 My Simulation

I'm a programmer, so I wrote a simulation program to help me figure out the answer to the riddle.

My program creates a salt shaker & a pepper shaker. It fills each with the appropriate compound. It copies a pinch of salt into the pepper shaker, shuffles it, & copies a pinch back. Then it prints the concentrations of each shaker. If the concentrations are not equal, it also prints a splat character (*).

My usual preference is Lisp, but I didn't have a Lisp system available at work, so I wrote the simulation in C.

The files for my program are these:

I didn't do the full ``./configure; make all check install'' trick. Instead, just edit the Makefile & then run make. That should get you a program called salt. Then run ./go, which will create three output files: out-010.txt, out-100.txt, & out-500.txt.

Two important functions in salt.c are S_Trial & S_Shuffle.

The S_Trial function does one experiment. It initializes the two containers, moves a pinch from the salt to the pepper, shakes the pepper (shuffles it), then moves a pinch back to the salt. It is the place to look for errors in my understanding of the riddle.

The S_Shuffle function randomizes the order of the elements in an array. If I shuffled the arrays incorrectly, the results will be incorrect. One coworker said this shuffle function was wrong, but a friend is a doctor of statistics, & he said it was right.

There is a bug, too. The program uses an asterisk (*) to mark the trials that had differing final concentrations. It compares the concentrations, but they are floating point numbers obtained from the S_Rate function, so round-off & other errors can happen. (I have seen them happen.) The program should instead compare the number of salt grains in the salt shaker to the number of pepper flakes in the pepper shaker. It could get those numbers from the S_Count function.

4 Analysis

The three output files from when I ran ./go are online. They are out-010.txt, out-100.txt, & out-500.txt.

Each of those files has 1,000 trials for containers that contain 1,000 grains of salt (or flecks or pepper), but each output file is for a unique pinch size. The first is for pinches of size 10. The second, for pinches of size 100. The third, for pinches of size 500, which is half of the container size.

To analyze the run, use ``grep \* |wc -l'' on each of the output files. (You'll need to pipe the output file through the grep, or you'll need to name the output file as the second argument to grep.) The number you'll see is the number of trials in that output file that did not result in a salt-to-pepper concentration in the salt shaker that was equal to the pepper-to-salt concentration in the pepper shaker. Since you know the number of trials in each file (1,000), & each file is for a unique pinch size, it's easy to compute the rate of unequal concentrations for the pinch size. I've done that & put the results in Table 1.


Table 1: The results of my simulation run
size of pinch unequals
10 20
100 115
500 382


5 Conclusion

Because the rate of occurences of trials with unequal final concentrations is so low (from 1 percent to 38 percent, & that is possibly with some falsely marked final concentrations), I suspect the answer to the riddle is that the final concentrations are the same.

Bibliography

Gene Michael Stover 2008-04-20