//------------------------------------------------------------------- // eulernum.cpp // // This program introduces the number e (Euler's Number) as // the limiting value for an infinite sequence of averages in // an experiment where real numbers are chosen at random from // the unit-interval [0,1] until their sum exceeds 1. As the // experiment is repeated, the average number of the summands // required approaches e = 2.718... , as is illustrated here // via computer simulations using one hundred million trials. // // to compile: $ g++ eulernum.cpp -o eulernum // to execute: $ ./eulernum // // NOTE: The program takes several seconds to finish running. // // ACKNOWLEDGEMENT: We wish to thank Professor Paul Zeitz for // introducing us to this intriguing problem. // // programmer: ALLAN CRUSE // written on: 06 FEB 2011 //------------------------------------------------------------------- #include // for printf() #include // for drand48(), srand48() #include // for time() #define N_TRIALS 100000000 // one hundred million double summands( void ) { double rep = 0.0, sum = 0.0; do { sum += drand48(); rep += 1.0; } while ( sum < 1.0 ); return rep; } int main( int argc, char **argv ) { printf( "\n An introduction to Euler's Number e = 2.718...\n\n" ); srand48( time( NULL ) ); for (int demo = 0; demo < 3; demo++) { double total = 0.0; double tries = 0.0; for (int i = 0; i < N_TRIALS; i++) { total += summands(); tries += 1.0; switch ( i+1 ) { case 1000: case 10000: case 100000: case 1000000: case 10000000: case 100000000: printf( " After %u trials, ", i+1 ); printf( "the average number of summands " ); printf( "was %1.3f \n", total / tries ); } } printf( "\n" ); } }