Re: neat way to program minimum of sum

*To*: mathgroup at smc.vnet.net*Subject*: [mg75175] Re: neat way to program minimum of sum*From*: Januk <ggroup at sarj.ca>*Date*: Thu, 19 Apr 2007 04:35:07 -0400 (EDT)*References*: <f01360$889$1@smc.vnet.net> <1176841580.125370.298150@n76g2000hsh.googlegroups.com> <9acbec9a0704180340g37a2f910yd08506db7708ba4f@mail.gmail.com>*Reply-to*: Januk <ggroup at sarj.ca>

Hi Przemyslaw, Yes, the random numbers are generated in the loop. However, you have the list of sums at each step, so extracting the x values is straight-forward: i = 1; f = NestWhileList[ Function[x, i++; x + Random[]], Random[], # < 10 &] xlist = Flatten@{First[f], Rest[f] - Most[f]} To better understand what has been calculated, evaluate the following to generate a table of results: TableForm[ Transpose[{Range[i], xlist, f}], TableHeadings -> {None, {"i", "x[i]", "Running Total"}} ] So to look at the fourth random value, use: xlist[[4]] To answer your questions, take a look at what NestWhileList does. The command has the following form: NestWhileList[ function, expression, test ] NestWhileList applies the function to expression and saves the result. If the test condition is still met, it will apply the function to the previous result and store the new result. Again, it checks the test function to determine if it should repeat applying the function to the latest result. Because we're using NestWhileList, the results from all iterations of the loop are returned in a list. So, the #<10& is a test function that checks that the current result is less than 10. The "#" and "&" construction is shorthand for a pure function. This is a function where # represents the argument to the function. Likewise, Function[x, i++;x+Random[]] is a function that calculates the next term in the sum. The function takes some argument, generates a random number, adds the two together and returns the result. The variable x is just a placeholder and has no meaning other than it represents the argument to the function. Hope that helps. Januk On Wednesday, April 18, 2007 at 12:40 GMT +0200, Przemyslaw Kaminski wrote: > Thanks for the answer. I can't test the code right now, but I don't > quite understand that Function[x, i++; x+Random[]] part... It seems > that the random values are generated inside the loop. What if I want > to process them later? How do I get, say, x[4]? > And what means '# < 10' ? Where's the parameter t > Regards, > P. Kaminski > PS I made a mistake posting my question. Should be > f[t]=min{n>0 : x[1] + .. + x[n] < t} > 2007/4/17, Januk <ggroup at sarj.ca>: >> Hi, >> >> You could try: >> >> i=1; >> NestWhileList[i = 1; >> f = NestWhileList[ >> Function[x, i++; x + Random[]], >> Random[], >> # < 10 &]; >> Transpose[{Range[i], f}] >> >> Good luck! >> >> >> On Apr 16, 8:08 pm, cge... at gmail.com (P. Kaminski) wrote: >> > Hi >> > I need to program the following function: >> > >> > f(t) = max{n>0 : X1 + ... + Xn > t} >> > >> > where Xi are some random numbers. I've done it using Module and Wile: >> > >> > x[i_]:=x[i]=Random[] >> > f[t_]:=f[t]=Module[{i}, i=1; While[Total[Array[x, i]]<=t, i++]; Return[i-1]]; >> > >> > This works but requires Module and three instructions within. Can it >> > be simplified to a true one-liner, preferably with some functional >> > programming trick? >> > >> > Thanks in advance, >> > P. Kaminski >> >> >>