Crashing Trains

A math puzzle has two trains on a railway track coming towards each other. They are 100 miles apart and are each going 50 miles per hour. They are going to crash. A bee tries to warn the engineers by flying back and forth multiple times between the converging trains, but the engineers never see the bee. The bee flips and pushes off taking no time to reverse course, like an Olympic swimmer. He is flying 75 miles per hour. The question is, “What is the total distance the bee flies before the trains crash?”

Someone asked Dr. John Von Neumann a similar question, and he paused a second and gave the correct answer. The questioner said, “Oh, you solved it the easy way.” The famous mathematician replied, “No, I did it the hard way!”

I used this problem teaching my programming classes. We would solve it the hard way. Students would learn about comparing floating point numbers, speed versus velocity, plotting data, animating the problem on the computer screen in character mode or graphics mode and debugging their programs.

I joked that if I ever teach this for a railroad, I will present it as two cruise ships and a dolphin. I did teach this for Norfolk Southern Railway’s newly hired programmers. We used the trains version of the project, which they enjoyed.

My students would have interesting bugs in their code. We would update the position of the trains and bee every simulated second. One student had the train on the left going 50 miles per hour and the train on the right going -50 mph, but he also would subtract rather than add. This made the train go backwards, and the trains stayed 100 miles apart. The bee had a lot of traveling to do. Another bug was to test for when the bee exactly touched a train instead of to test if sometime in the next second it would reach the train. That bug made the bee fly through the train and never reverse. A similar bug would also make the trains go through each other and never stop. Another bug made the bee go into a train and keep reversing direction, and getting trapped in the train.

Displaying the updated data every second would be overwhelming. We displayed the data whenever the bee would reverse direction and when the trains crashed. For the animation, we updated the display every simulated second. I got a lot of mileage out of this puzzle!

Diagram in text mode of railroad tracks, train1, the bee, and train2:


Our full scale diagram would have one equal sign per mile of track.