/* stickr.tc - rb - 2/1,4/2010 /* lrb - 2/4/2010 - fixed bug I introduced by replacing j/n with 0 in last line. /* it's possible to get all triangles, especially when you have low stick counts. /* good test script: .[int q;for (q=1;q<=5;++q) [main]] /* see http://yofx.blogspot.com/2009/03/probability-problem.html dpf int n,d,p [ /* Display proper fraction n/d as decimal number with p digit precision int w w=10*n/d if (p) [ MC w,14 dpf 10*n%d,d,p-1 /* recursive call ] ] stick [ /* Function used to determine whether three pieces of broken stick can form a triangle. int b1, b2, bx, l, c, r, t b2 = b1 = random 1, 180 while ( b1 == b2 ) /* Make sure b1 and b2 are different points on the stick b2 = random 1, 180 if ( b1 > b2 ) /* Make sure b2 is the larger number [ bx = b2 b2 = b1 b1 = bx ] r = 181 - b2 /* Right piece l = b1 /* Left piece c = 181 - l - r /* Center piece if (r < (l + c)) if (l < (c + r)) if (c < (l + r)) t = 1 /* The three pieces can form a triangle return t ] main [ /* Determine probability of forming triangle from three pieces of stick broken in two random places pl "";ps "stickr.tc - rb - 2/4/2010";pl "";pl "" int i, j, n ps "Enter a seed number. ==> ";last = gn pl "" if ( last%2 == 0 ) ++last /* make number odd seed = last ps "Enter number of sticks to break (1 to 3000). ==> ";n = gn pl "" if (n > 3000) n = 0 while (n < 1) [ ps "Invalid entry. Enter number between 1 and 3000. ==> ";n = gn pl "" if (n > 3000) n = 0 ] for ( i = 1; i <= n; ++i ) j = j + stick ps "Total sticks broken: ";pn n;pl "" ps "Total triangles formed: ";pn j;pl "" pl "Probability of triangle =";pn j/n;ps ".";dpf(j%n,n,4) pl "" ]