Re: how to sovle array which odd and even have difference recursive function
- To: mathgroup at smc.vnet.net
- Subject: [mg125266] Re: how to sovle array which odd and even have difference recursive function
- From: danl at wolfram.com
- Date: Sat, 3 Mar 2012 06:53:15 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jiqfv7$8d$1@smc.vnet.net>
On Friday, March 2, 2012 6:55:03 AM UTC-6, jie zhao wrote: > RSolve[{(b[n] == 1 + b[n/2] && Mod[n, 2] == 0) && (b[n] == 1/= n && > Mod[n, 2] == 1)}, b[n]==2, n] failed to work ! > when n is odd b[n] == 1/n > when n is even b[n] == 1 + b[n/2] Could do as follows. Solve a symbolic recurrence with base an arbitrary odd, and successive elements equal to powers of 2 times that base. So it will be a recurrence in two variables (more accurately, in one variable, parametrized by another). Then put this to use on an arbitrary input by breaking it into 2^power*odd. In[30]:= bsol[o_, p_] = First[RSolve[{b[odd, 0] == 1/odd, b[odd, pwr] == 1 + b[odd, pwr - 1]}, b[odd, pwr], {odd, pwr}]] /. {odd -> o, pwr -> p}; f[n_] := Module[{pwr = IntegerExponent[n, 2], odd}, odd = n/2^pwr; b[odd, pwr] /. bsol[odd, pwr] ] In[32]:= SetAttributes[f, Listable] In[33]:= f[Range[20]] Out[33]= {1, 2, 1/3, 3, 1/5, 4/3, 1/7, 4, 1/9, 6/5, 1/11, 7/3, 1/13, 8/7, 1/15, 5, 1/17, 10/9, 1/19, 11/5} Daniel Lichtblau Wolfram Research