Does my math/code check out?

A fly is doomed to meet its gristly end between two very large trains in a very small tunnel. The two trains start at different ends of a 100 mile tunnel and move toward eachother each going 25 mph. The fly starts from one of the trains the instant it starts moving and can accelerate (and decelerate) at 1 mi/hrsec. The fly is diginfied however and won't go down without a fight. It chooses to fly as far as possible in the little time it has left to it.** How far can it possibly go before it is crushed between the two colliding trains?
* There is no maximum speed, the fly can fly really, really fast.
** The fly must completely turn around and speed back up to 25 mph before it reaches the 2nd train, the fly may fly fast, but it's still easily killed if a train smacks it.
*** Also, ignore air currents, I know it's unrealistic for a fly to fly in the vacinity of a moving train, but so is a fly that can fly at over 500 mph.
Ignore magic numbers, i wanted to code this fast. the answer i came up with is 7,228.2274 Miles.
Imports System.IO
Imports System.ConsoleModule Module1
Sub Main()
Dim D As Double = (5280 * 100) 'Distance between trains in feet
Dim t As Integer = 0 ' time!
Dim Z As Double = 0 ' Fly Distance
Dim x As Double = 0 ' Fly Velocity
While (D > 0)
t = 0
Z = D
While (Z > (D / 2))
t = t + 1
Z = Z  x
x = x + (5279.4357 * t)
D = (D  (4400 / 60))
End While
Z = D
t = 0
While (Z <= D)
Z = Z + x
t = t + 1
x = x + (5279.4357 / t)
D = (D  (4400 / 60))
End While
Z = x
End While
WriteLine(Z)
ReadKey()
End Sub
End Module

Given that you're not doing a maximum speed, you might redo it using c, light speed.
Also given that light circles the planet 7.5 times per second, I think the answer is going to be a little bit more than 7200 miles.

But the fly starts out at a standstill and can speed up at a rate of 1 mile/hour a second.

Oah.
Of course.
Reading remains the hardest part of any assignment.

Guys i need to confess, i totally screwed up. here's the new, refined, working code, and the results:
Sub Main()
Dim D As Double = (5280 * 100) 'Distance between trains in feet
Dim t As Integer = 0 ' time!
Dim Z As Double = 0 ' Fly Distance in one round trip
Dim x As Double = 0 ' Fly Velocity
Dim totalD As Double = 0 ' cumulative distance of the fly
Dim backAndForth As Integer = 0 ' how many round trips the fly has made
While (D > 0)
t = 0
Z = D
While (Z > (D / 2))
t = t + 1
Z = Z  x
x = x + (5279.4357 * t)
D = (D  (4400 / 60))
totalD = totalD + x
End While
Z = D
t = 0
While (Z <= D)
Z = Z + x
t = t + 1
x = x + (5279.4357 / t)
D = (D  (4400 / 60))
totalD = totalD + x
End While
Z = x
backAndForth = backAndForth + 1
End While
WriteLine(totalD)
WriteLine(backAndForth)
ReadKey()
End SubOutputs... this is insane:
26,129,474.1 miles
3595 round trips
WOW!

Get rid of those short variable names. Bad style. You really want to memorize that "x" is the velocity and "z" is the distance?

And consider constants for the magic numbers... the compiler will inline them but you get a moniker to remember them by if you ever happen to code while drunk.

lewl... when i was explaining it to a non programmer friend, i had to say:
"GeneWitch: change the letter t into TimeElapsedInSeconds
GeneWitch: Change D into DistanceBetween2Trains
GeneWitch: change Z into TotalDistanceTheFlyHasFlown
GeneWitch: change x into HowFastTheFlyIsGoingInFeetPerSecond"But i commented the hell out of the code on my end. like i said i wanted the result fast, which meant after i did the math in notepad and calc.exe, i just threw in variable names as i wrote the code.
So ya, next time i open that .vb file, i'll use constants, change the FPS to miles per second, Change the variable names, etc.
This isn't production code, it was meant to do a calculation. my actual code is a lot better, I.E. in java:
HashMap hash1 = inHasher(args[0]); //sets hash1 to the hashtable generated by hasher(). clean coding, keeping main() clean.
String check; //this is the string i randomly create elsewhere
Word ret; // this is the string the hashtable returns
check = null; //check init
ret = null; //ret init
//int counter = 0; //this counts the number of words generated in each iteration of the inner while() loop. (debugging)
int wig = 0; //wig is the number of words we want to generate with the program
//int success = 0; //this variable is a cumulative count of iterations (debugging)
int nw = Integer.parseInt(args[1]);
int mi = Integer.parseInt(args[2]);
int ma = Integer.parseInt(args[3]);
while (wig < nw) //this outer while is set to generate (wig = X1) words that match a dictionary word.
{
//counter = 0; //init counter var(debugging)
split = Calendar.getInstance().getTimeInMillis();
check = null; //check will change every iteration many times... it is the random word.
ret = null; //ret = null if no match, else ret = the word that did match
while (ret == null) // this inner while is the heart of the program. gets a random word,etc. not MUCH better, but hella commented. i think every line has a comment (i was desperately trying to learn and remember java at the same time)

This reminds me of a story of a Famous Mathematician (I forget which Famous Mathematician; it may be untrue anyway.)
A friend posed the simplified version to the Famous Mathematician  the fly does not need to accelerate or decelerate; he just goes at a steady 50 MPH so you compute distance = rate * time. The FM immediately gave the correct answer.
The friend said, "That's right! You know, most people try to solve it by summing an infinite series in their heads."
FM: "That's what I did."

@newfweiler said:
This reminds me of a story of a Famous Mathematician (I forget which Famous Mathematician; it may be untrue anyway.)
A friend posed the simplified version to the Famous Mathematician  the fly does not need to accelerate or decelerate; he just goes at a steady 50 MPH so you compute distance = rate * time. The FM immediately gave the correct answer.
The friend said, "That's right! You know, most people try to solve it by summing an infinite series in their heads."
FM: "That's what I did."
My original answer to the question was "does he want to live the longest? then he travels 50 miles to the center of the tunnel and just sits down."
and according to my calcualtions, he can do that in roughly six seconds. not too shabby.

two things here:
1. If the fly can go 50 miles in six seconds your calculations are wrong. Accelerating at 1MPH/sec means that after 6 seconds it will be going 6 miles an hour.
2. The fly will not be able to move at all. If it starts from a train at a standstill and the train starts out moving at 25 MPH then the fly will immediately be smashed.

@tster said:
two things here:
1. If the fly can go 50 miles in six seconds your calculations are wrong. Accelerating at 1MPH/sec means that after 6 seconds it will be going 6 miles an hour.
2. The fly will not be able to move at all. If it starts from a train at a standstill and the train starts out moving at 25 MPH then the fly will immediately be smashed.
1 mile per second for the first second = 1 mile net distance.
2 miles per second second second = 3 miles
3 miles per second ... 7
4 miles per second 15
5 miles per second 31
6 miles per second > 50 miles covered at the end of six seconds. it's additive, isn't it?
maybe i am dumb
my algorithm does t * 1 mile = distance for that second. i thought that was the distance formula? It then adds that to the distance already travelled.
the trains aren't accelerating, so they're covering the same 73.333 feet per second combined...

@GeneWitch said:
@tster said:
two things here:
1. If the fly can go 50 miles in six seconds your calculations are wrong. Accelerating at 1MPH/sec means that after 6 seconds it will be going 6 miles an hour.
2. The fly will not be able to move at all. If it starts from a train at a standstill and the train starts out moving at 25 MPH then the fly will immediately be smashed.
1 mile per second for the first second = 1 mile net distance.
2 miles per second second second = 3 miles
3 miles per second ... 7
4 miles per second 15
5 miles per second 31
6 miles per second > 50 miles covered at the end of six seconds. it's additive, isn't it?
maybe i am dumb
my algorithm does t * 1 mile = distance for that second. i thought that was the distance formula? It then adds that to the distance already travelled.
the trains aren't accelerating, so they're covering the same 73.333 feet per second combined...
OK first off your original post said that the fly was accelerating at "1 mi/hr*sec*" What you just described is 1 mi/sec*sec. (although it's still wrong (more on this later))
Secondly, even if it is accelerating at that rate:
The fly and train start off at 0 at time 0. After .0001 second the train has moved 0.0073333336 feet. However, the fly has moved less than 5.2799995E5 feet. Fly is dead. Infact the fly died the instant that time starts.
As for your calculations above. You say the fly goes 1 mile in the first second. This isn't true. The fly is moving at 1m/sec at the end of 1 second, but during that second it was moving slower. There is a formula to calculate distance given constant acceleration but I'm too lazy (and I need to go take a test now) to find/derive it.
However, the point is you have to double integrate it. if acceleration = 1 and x = # of seconds, then velocity = x. If velocity = x then distance traveled = .5x^2. So after 1 second the fly has traveled .5 miles, after two seconds it's traveled 2 miles, after 3 it's traveled 4.5 miles; 4 > 8; 5>12.5; 6 > 18; 7 > 24.5.
Looks like I did have the time to derive it. Enjoy!

if you take 1 + 2 + ... + 6 /6 you get 3.5 (a cheap average velocity, because it scales up for every second passed, but just to show you)
v_{ave} = 3.5 mile/second = 5632.704 meter/second t = 6 second = 6 second which equals
distance (Δx) = 33796.224 meter which is 21 miles. averaging 3.5 velocity.
Considering that it is actually averaging more (you'd have to add averages for each second), and it isn't stable, it is linearly accelerating,
I'd buy 2x or more than 2x 21 miles over 6 seconds.
taking a larger snapshot, of say 20 seconds, average velocity is 10.5 which is 210 miles in 20 seconds.
going backwards, 210/20 = x/6 (20x = 1260 = 63 miles)
somewhere between 21, and 63 miles is the answer. 42 is the average. sounds like a nice place to be.
so like i said, around 6 seconds for 50 miles.
you can keep taking larger swaths of time to have a finer grain on the true average velocity of the fly, but it still comes out around the same. and seeing as how no one has contested my final values (yet!) i am hoping that the way i went about it is correct... just adding the distance covered in this second to the distance covered prior to this second.
Mind you, you have to deccelerate, and this is the only place where i didn't plug in a value appropriately... can you find where? and how much does that throw my mileage and round trip count off?

shit you might be right about the 1mi/hr per second. looks like i'll have to throw a divide by 60 in there. hm.
Plugging those values in really doesn't change much because of the relatively short amount of time spent accelerating before it has to deccelerate...
i get 3319 round trips, and 566241 miles traveled.
off by 1.08 and 46, respectively. Oops. Glad i have you folks around (also glad i don't do this crap for a living)
"I always mess up the decimal point!"

DELETING POST: we are posting at the same time

Are you saying using the correct formulas (derived from integrating) only changed it that much?

@tster said:
Are you saying using the correct formulas (derived from integrating) only changed it that much?
nah i plugged in the proper acceleration values. Hold on lemme grep this crap to a txt file.
You'll note that it takes 26 seconds to go 54.xxxx miles, then it has to deccelerate before it hits the other train. (also, i'm not entirely sure, but it might roll back at 26 seconds to 25 seconds. I forget how i coded it at this point, cause i was really trying to slap something together. I hath learneth my lesson.)
for the first acceleration run, printing
<FONT size=2>Writeline(totalD / 5280) ' distance in miles displayed, then newline
WriteLine(x) 'speed at this instant
WriteLine(t) ' time slice (in seconds)
we get:
0.0166648854166667
87.990595
1
0.0666595416666667
263.971785
2
0.166648854166667
527.94357
3
0.333297708333333
879.90595
4
0.583270989583333
1319.858925
5
0.933233583333333
1847.802495
6
1.399850375
2463.73666
7
1.99978625
3167.66142
8
2.74970609375
3959.576775
9
3.66627479166667
4839.482725
10
4.76615722916667
5807.37927
11
6.06601829166667
6863.26641
12
7.58252286458333
8007.144145
13
9.33233583333333
9239.012475
14
11.3321220833333
10558.8714
15
13.5985465
11966.72092
16
16.14827396875
13462.561035
17
18.997969375
15046.391745
18
22.1642976041667
16718.21305
19
25.6639235416667
18478.02495
20
29.5135120729167
20325.827445
21
33.7297280833333
22261.620535
22
38.3292364583333
24285.40422
23
43.3287020833333
26397.1785
24
48.74478984375
28596.943375
25
54.594164625
30884.698845
26
736217.30836496 ' ignore this number, it's a debugging writeline()
566241.849999957 ' final result in miles
3319 ' number of round trips

your calculations are off again. YOu are currently displaying the speed in ft/min not ft/sec which means you think the fly is traveling 60 times faster (and farther) than it really is...