how to iterate

  • Follow


hi all,

pls excuse my ignorance. i'm still new to mathematica, and i forgot
much of what i had learned.

how can i list the output of the function below for n=4..2006?
f(n) = ( f(n-1) + f(n-2) + 1 ) / f(n-3)
given f(1)=1, f(2)=2, f(3)=3.

thanks for any help in advance.

0
Reply valentino.rossi.85 (3) 4/24/2006 10:28:23 AM

Clear[f];
f[1]=1; f[2]=2; f[3]=3;
f[n_Integer?Positive]:=f[n]=(f[n-1]+f[n-2]+1)/f[n-3];

Table[{n,f[n]},{n,12}]

{{1, 1}, {2, 2}, {3, 3}, {4, 6}, {5, 5}, {6, 4}, {7, 5/3}, {8, 4/3}, {9, 1}, {10, 2}, {11, 
3}, 
  {12, 6}}


Bob Hanlon

> 
> From: "vr" <valentino.rossi.85@gmail.com>
> Subject:  how to iterate
> 
> hi all,
> 
> pls excuse my ignorance. i'm still new to mathematica, and i forgot
> much of what i had learned.
> 
> how can i list the output of the function below for n=4..2006?
> f(n) = ( f(n-1) + f(n-2) + 1 ) / f(n-3)
> given f(1)=1, f(2)=2, f(3)=3.
> 
> thanks for any help in advance.
> 
> 

0
Reply hanlonr (2281) 4/25/2006 9:24:53 AM


vr wrote:
> hi all,
> 
> pls excuse my ignorance. i'm still new to mathematica, and i forgot
> much of what i had learned.
> 
> how can i list the output of the function below for n=4..2006?
> f(n) = ( f(n-1) + f(n-2) + 1 ) / f(n-3)
> given f(1)=1, f(2)=2, f(3)=3.
> 
> thanks for any help in advance.
> 

I think something like
f[n_] := f[n] = ( f[n-1] + f[n-2] + 1 ) / f[n-3]
MyTable = Table[f[n],{n,4,2006}]

might help.

/ johan

0
Reply johan.gronqvist (27) 4/25/2006 9:28:56 AM

Hello Valentino
You don't need to list the values of your sequence f(n) .It is
periodic:
f[1] = 1;
f[2] = 2;
f[3] = 3;
f[n_] := (f[n - 1] + f[n - 2] + 1)/f[n - 3].
This gives f(9)=1;f(10)=2; f(11)=3 and then the period is given by f(i)
for i=1,...8.
Was that the real problem?
Richard Kausch  Paris (F)

0
Reply rkausch (7) 4/25/2006 9:35:01 AM

vr wrote:
> hi all,
> 
> pls excuse my ignorance. i'm still new to mathematica, and i forgot
> much of what i had learned.
> 
> how can i list the output of the function below for n=4..2006?
> f(n) = ( f(n-1) + f(n-2) + 1 ) / f(n-3)
> given f(1)=1, f(2)=2, f(3)=3.
> 
> thanks for any help in advance.
> 

f[1] = 1; f[2] = 2; f[3] = 3;
f[(n_Integer)?Positive] := f[n] = (f[n - 1] + f[n - 2] + 1)/f[n - 3];
Table[f[n], {n, 4, 2006}]

HTH,
Jean-Marc

0
Reply jeanmarc.gulliet (2157) 4/25/2006 9:37:03 AM

vr wrote:
> hi all,
>
> pls excuse my ignorance. i'm still new to mathematica, and i forgot
> much of what i had learned.
>
> how can i list the output of the function below for n=4..2006?
> f(n) = ( f(n-1) + f(n-2) + 1 ) / f(n-3)
> given f(1)=1, f(2)=2, f(3)=3.
>
> thanks for any help in advance.

In[1]:= Short@InputForm@
Nest[Append[#,(Plus@@Take[#,-2]+1)/#[[-3]]]&, {1,2,3}, 2003]

Out[1]//Short=
{1, 2, 3, 6, 5, 4, 5/3, 4/3, 1, 2, 3, <<1990>>, 2, 3, 6, 5, 4}

Remove the "Short@" to see all the terms.

0
Reply koopman (743) 4/25/2006 9:39:05 AM

On 4/24/06 at 6:02 AM, valentino.rossi.85@gmail.com (vr) wrote:

>pls excuse my ignorance. i'm still new to mathematica, and i forgot
>much of what i had learned.

>how can i list the output of the function below for n=4..2006? f(n)
>= ( f(n-1) + f(n-2) + 1 ) / f(n-3) given f(1)=1, f(2)=2, f(3)=3.

There are a number of ways to achieve this. One of the simpler is:

Table[(f[n-1]+f[n-2]+1)/f[n-3], {n, 4, 2006}]

another way to get the same result would be

(f[#-1]+f[#-2]+1)/f[#-3]&/@Range[4,2006]
--
To reply via email subtract one hundred and four

0
Reply readnewsciv (914) 4/25/2006 9:40:05 AM

I would iterate with a recursive definition, like this:

f[n_] := (f[n - 1] + f[n - 2] + 1)/f[n - 3];
f[1] = 1;
f[2] = 2;
f[3] = 3;

In[5]:=
f[10]

Out[5]=
2

(Valuating at: 1 ... 23)

In[8]:=
AbsoluteTiming[f/@Range[23]]

Out[8]=
{2.37 Second, {1, 2, 3, 6, 5, 4, 5/3, 4/3, 1, 2, 3, 6, 5, 4, 5/3, 4/3,
1, 2, 3, 6, 5, 4, 5/3}}

Note the calculation time. - Why does it take that much? For an initial
n, the expression inflates recursively, making n smaller, until n drops
below 3, when specific values are used. For a new n, the story is
similar. You can't get far like this.

You can make it faster by storing the known values along the way. With
a slight modification of the above definition: in addition to
SetDelayed, ":=", a normal Set, "=", is used, which stores the values
along the way, and that are later. You calculate g[4] for example, the
result is stored, and used when you want to calculate g[5], and so on.
Calculating from 4 on, one by one, nothing is truly "recursive"
anymore. But you can go far now.

g[n_] := g[n] = (g[n - 1] + g[n - 2] + 1)/g[n - 3];
g[1] = 1;
g[2] = 2;
g[3] = 3;

In[45]:=
AbsoluteTiming[g/@Range[2006];]

Out[45]=
{0.0468750 Second,Null}

Bye,
Borut Levart
Slovenia

0
Reply BoLe791 (40) 4/25/2006 9:47:11 AM

Hello
The iterations proposed in different previous mails are very long to
perform .
I suggest
g[{a_, b_, c_}] = {b, c, (1 + b + c)/a};
Take[Flatten[NestList[g, {1, 2, 3}, 500]], {1, 10000, 3}].
This gives the 10000 first terms in some ms.
Anyway this sequence is periodic byt this technique may reveal useful
in any other context.
Richard Kausch .Paris (F)

0
Reply rkausch (7) 4/26/2006 9:09:54 AM

Your solution is just a bit different representation, but neat! I like
it.

0
Reply BoLe791 (40) 4/27/2006 6:29:14 AM

Hi.

f[1] = 1; f[2] = 2; f[3] = 3;


Table[f[n] = (f[n - 1] + f[n - 2] + 1)/f[n - 3], {n, 4, 17}]
{6, 5, 4, 5/3, 4/3, 1, 2, 3, 6, 5, 4, 5/3, 4/3, 1}

?f

Note that there are only 8 unique outputs, as the output pattern  repeats.

You could use

Mod[n, 8, 1]

on any integer n to convert it to a number 1-8, and select one of your 8 
outputs.

-- 
HTH.  :>)

Dana DeLouis
Windows XP, Office 2003, Mathematica 5.2



"vr" <valentino.rossi.85@gmail.com> wrote in message 
news:e2i987$9fc$1@smc.vnet.net...
> hi all,
>
> pls excuse my ignorance. i'm still new to mathematica, and i forgot
> much of what i had learned.
>
> how can i list the output of the function below for n=4..2006?
> f(n) = ( f(n-1) + f(n-2) + 1 ) / f(n-3)
> given f(1)=1, f(2)=2, f(3)=3.
>
> thanks for any help in advance.
> 

0
Reply ddelouis (10) 6/2/2006 8:25:28 AM

10 Replies
35 Views

(page loaded in 0.155 seconds)


Reply: