Post Reply 
Recursive sequences in CAS on prime.
06-19-2015, 07:22 AM
Post: #1
Recursive sequences in CAS on prime.
Hello

I can't figure out how to define a recursive sequence in CAS on the HP prime.
I want to define:
u(1)=3/2
u(2)=5/3
u(n)=2003-6002/u(n-1)+4000/(u(n-1)u(n-2))

Of course, I can define this sequence in the sequence app in HOME mode but it gives wrong answers (the sequence is made for that purpose Smile ) so I want to have exact values in CAS.

I made a CAS program with a FOR loop and it works fine but I'm looking for a more straightforward way to do this.
In XCAS, I can type in
Code:
u(n):={si n==1 alors 3/2; sinon si n==2 alors 5/3; sinon 2003-6002/u(n-1)+4000/(u(n-1)*u(n-2)); fsi; fsi;}
and it works.
I am looking for a similar syntax working on the prime too.

thank you for your help.
Find all posts by this user
Quote this message in a reply
06-19-2015, 09:01 AM
Post: #2
RE: Recursive sequences in CAS on prime.
Here is a solution:
Code:
#cas
mf(n):=
BEGIN
IF n==1 THEN
RETURN(3/2);
ELSE
IF n==2 THEN
RETURN(5/3);
END;
RETURN(2003-6002/mf(n-1)+4000/(mf(n-1)*mf(n-2)));
END;


END;
#end
which seems to work in your desired way. It provides exact values at least until n=10, now I am trying 25 and this runs now the fifth minute on the emulator which is due to the 3 recursive calls in the code, so I cancelled it.
Arno
Find all posts by this user
Quote this message in a reply
06-19-2015, 09:20 AM
Post: #3
RE: Recursive sequences in CAS on prime.
Thank you. This is a way to do this. But is there another way, without making a CAS program ? with a declaration like u(n):= ... ?
Find all posts by this user
Quote this message in a reply
06-19-2015, 10:33 AM
Post: #4
RE: Recursive sequences in CAS on prime.
Hello Arne,

maybe your program runs faster, if you create two local variables, wich get the values of mf(n-1) and mf(n-2), so you have to do the recursive call only twice ind not three times.

regards

Wolfgang
Find all posts by this user
Quote this message in a reply
06-19-2015, 02:12 PM
Post: #5
RE: Recursive sequences in CAS on prime.
(06-19-2015 10:33 AM)ww63 Wrote:  Hello Arne,

maybe your program runs faster, if you create two local variables, wich get the values of mf(n-1) and mf(n-2), so you have to do the recursive call only twice ind not three times.

regards

Wolfgang

Yes, you are right, a reduction of recursive calls is necessary. But your solution, I think, will not work with local variables as they are created each time when the function is called. Here additional Parameters are necessary to pass the last two values and then the solution with a for-loop seems better.
Arno
Find all posts by this user
Quote this message in a reply
06-19-2015, 04:37 PM
Post: #6
RE: Recursive sequences in CAS on prime.
I managed to declare the sequence directly in CAS:

Code:
u:=(n)->CASE IF (n==1) THEN 3/2 END IF (n==2) THEN 5/3 END IF n>2 THEN 2003-6002/u(n-1)+4000/(u(n-1)*u(n-2)) END END

It works fine until n>10. After this, the recursion is too deep.

I made a CAS program too with a FOR loop :
Code:

#cas
u(n):=
BEGIN
LOCAL r,r1,r2
CASE
IF n==1 THEN RETURN 3/2; END;
IF n==2 THEN RETURN 5/3; END;
DEFAULT
r1=3/2
r2=5/3
FOR I FROM 3 TO n DO
r := 2003-6002/r2+4000/(r1*r2);
r1:=r2;
r2:=r;
END;

RETURN r;
END;
#end

But after all this, I think my favorite method is to use the spreadsheet with cells in CAS mode. It is fast and quite easy.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)