Re: Recursive function
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Recursive function
- From: colinr at sue.econ.su.oz.au (Colin Rose)
- Date: Tue, 28 Sep 93 2:35:30 EET
>> I wish to define a recursive function with conditional special cases:
>> f[n_, a_] := f[n-1,a] + f[n-1,a - n]
>>
>> s.t.: (1) f[n,0] = 1
>> (2) If a<0 then f[n,a] = 0
>> (3) If a >= n (n-1)/2 then f[n,a] = f[n,n(n+1)/2]
One neat way of doing this is to use Which:
f[n_, a_] := Which[ a == 0, 1,
a < 0, 0,
a >= n(n-1)/2, f[n, n(n+1)/2],
True, f[n-1, a] + f[n-1, a-n] ]
Regards
Colin
P.S. The function is 'non-convergent', as is clear from examining f[1, 1].
Colin Rose
Dept. of Economics
University of Sydney
colinr at extro.ucc.su.oz.au
*************************