*One train leaves Los Angeles at 15mph heading for New York. Another train leaves from New York at 20mph heading for Los Angeles on the same track. If a bird, flying at 25mph, leaves from Los Angeles at the same time as the train and flies back and forth between the two trains until they collide, how far will the bird have traveled?*

Ok, I developed a ad-hoc program to help solve this problem. The major variable in the question is the distance. The distance is the variable, in which just complicates the problem. I know the distance is greater 2000, so I tested with the values 2000, 2500, and 3000.

int NYLADistance = 2000;

int LATrainSpeed = 15;

int NYTrainSpeed = 20;

int BirdSpeed = 25;

int LAStart = NYLADistance;

int NYStart = 0;

int BirdStart = NYLADistance;

int hour = 0;

int BirdTrips = 0;

bool FlyingEast = true;

int TotalBirdTravel = 0;

while(LAStart >= NYStart)

{

hour++;

LAStart = LAStart - LATrainSpeed;

NYStart = NYStart + NYTrainSpeed;

if(FlyingEast)

{

BirdStart = BirdStart - BirdSpeed;

if(BirdStart < NYStart)

{

FlyingEast = false;

BirdTrips++;

}

}

else

{

BirdStart = BirdStart + BirdSpeed;

if(BirdStart > LAStart)

{

FlyingEast = true;

BirdTrips++;

}

}

TotalBirdTravel = TotalBirdTravel + BirdSpeed;

}

I came up with these values:

Distance 3000, bird flys 2150

Distance 2500, bird flys 1800

Distance 2000, bird flys 1450

So, by using these numbers I came up with the formula:

y = (7/10)x + 50

y is the bird travel

x is the distance from LA to NY

## 2 comments:

This is a well known puzzle - not a test of programming skills but of ability to reason mathematically. It takes d/(s1 + s2) hours for the crash to occur, where d is the distance and s1 and s2 are the speeds of the trains. During that amount of time the bird will fly the number of hours times the speed of the bird. So an exact solution is d * 25/(15+20) = d*5/7. Think first - then program.

Thanks for your answer, I was just too lazy to come up this solution the first time through, since I just woke up, so I just let the computer do all the thinking for me. I came close to the real solution though.

Distance 3000, me: 2150, ans: 2142

Distance 2500, me: 1800, ans: 1785

Distance 2000, me: 1450, ans: 1428

Post a Comment