Pell Sequence

Math 242 Modern Computational Math, spring 2021

First we will define a module that generates a recursive sequence. The following module generateSequence takes three parameters and returns a list.

Specifically, generateSequence[(Subscript[a, 0]), (Subscript[a, 1]), n] returns a list of n≥ 2 terms. The first two terms are (Subscript[a, 0]) and (Subscript[a, 1]); all other terms are computed by the recurrence (Subscript[a, n])=2(Subscript[a, n-1])+(Subscript[a, n-2]).

generateSequence[a_,b_,n_]:=Module[
{seq={a, b}, val1=a, val2=b,next, m=2},
If[n ≤ 2, Return[seq]];
Do[
next=m*val2+val1;            (* compute the next value *)
seq=Append[seq,next];     (* append the next value to the sequence *)
val1=val2;                                (* shift val2 to val1 *)
val2=next;                                 (* shift next to val2 *)
,n-2];
seq      (* return the sequence *)
]

Note: This module doesn’t simply return the nth term of a sequence. It returns a list of n terms.

For example, we can generate the first ten terms of the Pell sequence:

Or we can generate the first ten terms of the Pell-Lucas sequence: