Re: neat way to program minimum of sum
- To: mathgroup at smc.vnet.net
- Subject: [mg75139] Re: [mg75072] neat way to program minimum of sum
- From: anguzman at ing.uchile.cl
- Date: Wed, 18 Apr 2007 05:03:55 -0400 (EDT)
- References: <200704170008.UAA08409@smc.vnet.net>
Hello: I think you mean: f(t) = min{n>0 : X1 + ... + Xn > t} I believe that your current implementation calls Random[] too many times. That is, it tries with a random list of "i" elements and if it doesn't fullfill the request in the while loop, generates another list of "i+1" elements. Unless this feature is absolutely necessary I think that a more compact and efficient way could be this recursive definition: f[t_?(# < 0 &)] = 0 f[t_?(#1 >= 0 & )] := 1 + f[t - Random[]]; It calls Random[] the strictly necessary number of times. The only problem is that it's defined as zero for negative values. I believe that these variables are called negative binomials. Hope this could help Atte. Andres Guzman P. Kaminski" <cgenie at gmail.com> ha escrito: > 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++]; Re= turn[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 > > ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.
- References:
- neat way to program minimum of sum
- From: cgenie@gmail.com (P. Kaminski)
- neat way to program minimum of sum