       Re: Finding length in recursive definition?

• To: mathgroup at smc.vnet.net
• Subject: [mg60924] Re: Finding length in recursive definition?
• From: "Scout" <mathem at tica.org>
• Date: Tue, 4 Oct 2005 01:24:53 -0400 (EDT)
• References: <dhlcj2\$d16\$1@smc.vnet.net> <dhntbj\$1j9\$1@smc.vnet.net> <dhqp87\$g8\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Hi Peter,
I hope you will accept this simple trick ;-)

In[]:= DownValues[f][[-3]]/._[_[_[n_]],r_]:>n

~Scout~

> Scout schrieb:
>> Hi Jose,
>> If I've well understood your question
>> about how many values of a recursive function f[] are stored in memory,
>> you can try this:
>>
>>     Length[DownValues[f]] - 1
>>
>> where -1 counts the definition of f[] itself.
>>
>>     ~Scout~
>>
>> "Jose Reckoner"
>>
>>>I have something like:
>>>f = 1
>>>f = 3
>>>f[n_] := f[n] = f[n - 1] + f[n - 2]
>>>
>>>and in the course of work, f[n] gets evaluated an unknown number of
>>>times resulting in
>>>
>>>
>>>>>?f
>>>
>>>f = 1
>>>f = 3
>>>f = 4
>>>f[n_] := f[n] = f[n - 1] + f[n - 2]
>>>
>>>I want to figure out the greatest integer n such that f[n] has already
>>>been computed and is stored. In this case, it is 3.
>>>
>>>How can I do this?
>>>
>>>Thanks!
>>>
>>>Jose
>>>
>>
>>
>
> Hi ~Scout~,
>
> what would you do for this function:
>
> In:=
> Clear[f];
> f=1; f=1;
> f[n_Integer?EvenQ]:=f[n]=1+f[n/2];
> f[n_]:=f[n]=f[n-1]-1;
>
> In:= f
> Out= 3
>
> In:= Definition[f]//InputForm
> Out//InputForm=
> f = 1
> f = 1
> f = 2
> f = 3
> f = 4
> f = 3
> f[(n_Integer)?EvenQ] := f[n] = 1 + f[n/2]
> f[n_] := f[n] = f[n - 1] - 1
>
> ?
>
> Peter
>

```

• Prev by Date: Re: ImplicitPlot in 3D of several equations
• Next by Date: Re: BinCounts
• Previous by thread: Re: Finding length in recursive definition?
• Next by thread: Re: Finding length in recursive definition?