Re: neat way to program minimum of sum

*To*: mathgroup at smc.vnet.net*Subject*: [mg75197] Re: neat way to program minimum of sum*From*: Norbert Marxer <marxer at mec.li>*Date*: Fri, 20 Apr 2007 00:37:29 -0400 (EDT)*References*: <f01360$889$1@smc.vnet.net><f07agh$4gm$1@smc.vnet.net>

On 19 Apr., 10:50, CoolGenie <CGe... at gmail.com> wrote: > On 18 Kwi, 11:09, Norbert Marxer <mar... at mec.li> wrote: > > > f[t_] := Length@NestWhileList[( > > s = s + Random []) &, s = Random[], (s < t) &] - 1 > > This is nice but not satisfactory. The statement is that I have the > values x[1], x[2], ... given first and then based on these I want to > generate the minimum (should be minimum there, not maximum). I think > that the code you suggested will give different answer every time... > If I'm wrong please correct me. > > P. Kaminski Hello My solution (as you state correctly) does not build up the array of random numbers at the beginning, but calculates it from scratch each time you call the function f (Ray Koopman did the same in his post). This will give you a different solution each time. You can do a statistical analysis and calculate (e.g.) the average number of random numbers which will add up to your input number for f. If you require to have the same random numbers each time you could use SeedRandom[1] when starting NestWhile. If you combine the solution given by Daniel Lichtblau and the one given by Ray Koopman (which is similar to mine) you would neither build up the x array nor build up the list of partial sums. This would probably be the best solution with respect to memory. As always there are many ways to write a program and it depends on your requirements (efficient code, short code, understandable code, memory requirements etc.). You know best what you want. I hope the different proposals which are offered above are of any help to you. Best Regards Norbert Marxer