MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: neat way to program minimum of sum

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75133] Re: neat way to program minimum of sum
  • From: Ray Koopman <koopman at sfu.ca>
  • Date: Wed, 18 Apr 2007 05:00:50 -0400 (EDT)
  • References: <f01360$889$1@smc.vnet.net>

On Apr 16, 5: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

f[t_] := Length@NestWhileList[#-Random[]&, t, Positive] - 2



  • Prev by Date: how to get the table
  • Next by Date: Eliminate white border around DensityPlot?
  • Previous by thread: Re: neat way to program minimum of sum
  • Next by thread: Re: neat way to program minimum of sum