# Lucas sequence

In mathematics, the Lucas sequences $U_{n}(P,Q)$ and $V_{n}(P,Q)$ are certain constant-recursive integer sequences that satisfy the recurrence relation
    $x_{n}=P\cdot x_{n-1}-Q\cdot x_{n-2}$
where $P$ and $Q$ are fixed integers. Any sequence satisfying this recurrence relation can be represented as a linear combination of the Lucas sequences $U_{n}(P,Q)$ and $V_{n}(P,Q).$
More generally, Lucas sequences $U_{n}(P,Q)$ and $V_{n}(P,Q)$ represent sequences of polynomials in $P$ and $Q$ with integer coefficients.
Famous examples of Lucas sequences include the Fibonacci numbers, Mersenne numbers, Pell numbers, Lucas numbers, Jacobsthal numbers, and a superset of Fermat numbers (see below). Lucas sequences are named after the French mathematician Édouard Lucas.

## Recurrence relations

Given two integer parameters $P$ and $Q$, the Lucas sequences of the first kind $U_{n}(P,Q)$ and of the second kind $V_{n}(P,Q)$ are defined by the recurrence relations:
    ${\begin{aligned}U_{0}(P,Q)&=0,\\U_{1}(P,Q)&=1,\\U_{n}(P,Q)&=P\cdot U_{n-1}(P,Q)-Q\cdot U_{n-2}(P,Q){\mbox{ for }}n>1,\end{aligned}}$
and
    ${\begin{aligned}V_{0}(P,Q)&=2,\\V_{1}(P,Q)&=P,\\V_{n}(P,Q)&=P\cdot V_{n-1}(P,Q)-Q\cdot V_{n-2}(P,Q){\mbox{ for }}n>1.\end{aligned}}$
It is not hard to show that for $n>0$,
    ${\begin{aligned}U_{n}(P,Q)&={\frac {P\cdot U_{n-1}(P,Q)+V_{n-1}(P,Q)}{2}},\\V_{n}(P,Q)&={\frac {(P^{2}-4Q)\cdot U_{n-1}(P,Q)+P\cdot V_{n-1}(P,Q)}{2}}.\end{aligned}}$
The above relations can be stated in matrix form as follows:
    ${\begin{bmatrix}U_{n}(P,Q)\\U_{n+1}(P,Q)\end{bmatrix}}={\begin{bmatrix}0&1\\-Q&P\end{bmatrix}}\cdot {\begin{bmatrix}U_{n-1}(P,Q)\\U_{n}(P,Q)\end{bmatrix}},$
    ${\begin{bmatrix}V_{n}(P,Q)\\V_{n+1}(P,Q)\end{bmatrix}}={\begin{bmatrix}0&1\\-Q&P\end{bmatrix}}\cdot {\begin{bmatrix}V_{n-1}(P,Q)\\V_{n}(P,Q)\end{bmatrix}},$
    ${\begin{bmatrix}U_{n}(P,Q)\\V_{n}(P,Q)\end{bmatrix}}={\begin{bmatrix}P/2&1/2\\(P^{2}-4Q)/2&P/2\end{bmatrix}}\cdot {\begin{bmatrix}U_{n-1}(P,Q)\\V_{n-1}(P,Q)\end{bmatrix}}.$

## Examples

Initial terms of Lucas sequences $U_{n}(P,Q)$ and $V_{n}(P,Q)$ are given in the table:
    ${\begin{array}{r|l|l}n&U_{n}(P,Q)&V_{n}(P,Q)\\\hline 0&0&2\\1&1&P\\2&P&{P}^{2}-2Q\\3&{P}^{2}-Q&{P}^{3}-3PQ\\4&{P}^{3}-2PQ&{P}^{4}-4{P}^{2}Q+2{Q}^{2}\\5&{P}^{4}-3{P}^{2}Q+{Q}^{2}&{P}^{5}-5{P}^{3}Q+5P{Q}^{2}\\6&{P}^{5}-4{P}^{3}Q+3P{Q}^{2}&{P}^{6}-6{P}^{4}Q+9{P}^{2}{Q}^{2}-2{Q}^{3}\end{array}}$

## Explicit expressions

The characteristic equation of the recurrence relation for Lucas sequences $U_{n}(P,Q)$ and $V_{n}(P,Q)$ is:
    $x^{2}-Px+Q=0\,$
It has the discriminant $D=P^{2}-4Q$ and the roots:
    $a={\frac {P+{\sqrt {D}}}{2}}\quad {\text{and}}\quad b={\frac {P-{\sqrt {D}}}{2}}.\,$
Thus:
    $a+b=P\,,$
    $ab={\frac {1}{4}}(P^{2}-D)=Q\,,$
    $a-b={\sqrt {D}}\,.$
Note that the sequence $a^{n}$ and the sequence $b^{n}$ also satisfy the recurrence relation. However these might not be integer sequences.

### Distinct roots

When $D\neq 0$, a and b are distinct and one quickly verifies that
    $a^{n}={\frac {V_{n}+U_{n}{\sqrt {D}}}{2}}$
    $b^{n}={\frac {V_{n}-U_{n}{\sqrt {D}}}{2}}.$
It follows that the terms of Lucas sequences can be expressed in terms of a and b as follows
    $U_{n}={\frac {a^{n}-b^{n}}{a-b}}={\frac {a^{n}-b^{n}}{\sqrt {D}}}$
    $V_{n}=a^{n}+b^{n}\,$

### Repeated root

The case $D=0$ occurs exactly when $P=2S{\text{ and }}Q=S^{2}$ for some integer S so that $a=b=S$. In this case one easily finds that
    $U_{n}(P,Q)=U_{n}(2S,S^{2})=nS^{n-1}\,$
    $V_{n}(P,Q)=V_{n}(2S,S^{2})=2S^{n}.\,$

## Properties
### Generating functions

The ordinary generating functions are
    $\sum _{n\geq 0}U_{n}(P,Q)z^{n}={\frac {z}{1-Pz+Qz^{2}}};$
    $\sum _{n\geq 0}V_{n}(P,Q)z^{n}={\frac {2-Pz}{1-Pz+Qz^{2}}}.$

### Pell equations

When $Q=\pm 1$, the Lucas sequences $U_{n}(P,Q)$ and $V_{n}(P,Q)$ satisfy certain Pell equations:
    $V_{n}(P,1)^{2}-D\cdot U_{n}(P,1)^{2}=4,$
    $V_{n}(P,-1)^{2}-D\cdot U_{n}(P,-1)^{2}=4(-1)^{n}.$

### Relations between sequences with different parameters

* For any number c, the sequences $U_{n}(P',Q')$ and $V_{n}(P',Q')$ with
       $P'=P+2c$
       $Q'=cP+Q+c^{2}$
    have the same discriminant as $U_{n}(P,Q)$ and $V_{n}(P,Q)$:
       $P'^{2}-4Q'=(P+2c)^{2}-4(cP+Q+c^{2})=P^{2}-4Q=D.$
* For any number c, we also have
       $U_{n}(cP,c^{2}Q)=c^{n-1}\cdot U_{n}(P,Q),$
       $V_{n}(cP,c^{2}Q)=c^{n}\cdot V_{n}(P,Q).$

### Other relations

The terms of Lucas sequences satisfy relations that are generalizations of those between Fibonacci numbers $F_{n}=U_{n}(1,-1)$ and Lucas numbers $L_{n}=V_{n}(1,-1)$. For example:
    ${\begin{array}{r|l}{\text{General case}}&(P,Q)=(1,-1),D=P^{2}-4Q=5\\\hline DU_{n}={V_{n+1}-QV_{n-1}}=2V_{n+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ots &2^{n-1}F_{n}={n \choose 1}+5{n \choose 3}+\cdots \\2^{n-1}V_{n}=P^{n}+{n \choose 2}P^{n-2}D+{n \choose 4}P^{n-4}D^{2}+\cdots &2^{n-1}L_{n}=1+5{n \choose 2}+5^{2}{n \choose 4}+\cdots \end{array}}$

### Divisibility properties

Among the consequences is that $U_{km}(P,Q)$ is a multiple of $U_{m}(P,Q)$, i.e., the sequence $(U_{m}(P,Q))_{m\geq 1}$ is a divisibility sequence. This implies, in particular, that $U_{n}(P,Q)$ can be prime only when n is prime. Another consequence is an analog of exponentiation by squaring that allows fast computation of $U_{n}(P,Q)$ for large values of n. Moreover, if $\gcd(P,Q)=1$, then $(U_{m}(P,Q))_{m\geq 1}$ is a strong divisibility sequence.
Other divisibility properties are as follows:
* If n is an odd multiple of m, then $V_{m}$ divides $V_{n}$.
* Let N be an integer relatively prime to 2Q. If the smallest positive integer r for which N divides $U_{r}$ exists, then the set of n for which N divides $U_{n}$ is exactly the set of multiples of r.
* If P and Q are even, then $U_{n},V_{n}$ are always even except $U_{1}$.
* If P is odd and Q is even, then $U_{n},V_{n}$ are always odd for every $n>0$.
* If P is even and Q is odd, then the parity of $U_{n}$ is the same as n and $V_{n}$ is always even.
* If P and Q are odd, then $U_{n},V_{n}$ are even if and only if n is a multiple of 3.
* If p is an odd prime, then $U_{p}\equiv \left({\tfrac {D}{p}}\right),V_{p}\equiv P{\pmod {p}}$ (see Legendre symbol).
* If p is an odd prime which divides P and Q, then p divides $U_{n}$ for every $n>1$.
* If p is an odd prime which divides P but not Q, then p divides $U_{n}$ if and only if n is even.
* If p is an odd prime which divides Q but not P, then p never divides $U_{n}$ for any $n>0$.
* If p is an odd prime which divides D but not PQ, then p divides $U_{n}$ if and only if p divides n.
* If p is an odd prime which does not divide PQD, then p divides $U_{l}$, where $l=p-\left({\tfrac {D}{p}}\right)$.
The last fact generalizes Fermat's little theorem. These facts are used in the Lucas–Lehmer primality test. Like Fermat's little theorem, the converse of the last fact holds often, but not always; there exist composite numbers n relatively prime to D and dividing $U_{l}$, where $l=n-\left({\tfrac {D}{n}}\right)$. Such composite numbers are called Lucas pseudoprimes.
A prime factor of a term in a Lucas sequence which does not divide any earlier term in the sequence is called primitive. Carmichael's theorem states that all but finitely many of the terms in a Lucas sequence have a primitive prime factor. Indeed, Carmichael (1913) showed that if D is positive and n is not 1, 2 or 6, then $U_{n}$ has a primitive prime factor. In the case D is negative, a deep result of Bilu, Hanrot, Voutier and Mignotte shows that if n > 30, then $U_{n}$ has a primitive prime factor and determines all cases $U_{n}$ has no primitive prime factor.

## Specific names

The Lucas sequences for some values of P and Q have specific names:
    U$_n$(1, −1) : Fibonacci numbers
    V$_n$(1, −1) : Lucas numbers
    U$_n$(2, −1) : Pell numbers
    V$_n$(2, −1) : Pell–Lucas numbers (companion Pell numbers)
    U$_n$(1, −2) : Jacobsthal numbers
    V$_n$(1, −2) : Jacobsthal–Lucas numbers
    U$_n$(3, 2) : Mersenne numbers 2$^n$ − 1
    V$_n$(3, 2) : Numbers of the form 2$^n$ + 1, which include the Fermat numbers
    U$_n$(6, 1) : The square roots of the square triangular numbers.
    U$_n$(x, −1) : Fibonacci polynomials
    V$_n$(x, −1) : Lucas polynomials
    U$_n$(2x, 1) : Chebyshev polynomials of second kind
    V$_n$(2x, 1) : Chebyshev polynomials of first kind multiplied by 2
    U$_n$(x+1, x) : Repunits in base x
    V$_n$(x+1, x) : x$^n$ + 1
Some Lucas sequences have entries in the On-Line Encyclopedia of Integer Sequences:
    $P\,$$Q\,$$U_{n}(P,Q)\,$$V_{n}(P,Q)\,$ −13OEIS: A214733 1−1OEIS: A000045OEIS: A000032 11OEIS: A128834OEIS: A087204 12OEIS: A107920OEIS: A002249 2−1OEIS: A000129OEIS: A002203 21OEIS: A001477OEIS: A007395 22OEIS: A009545 23OEIS: A088137 24OEIS: A088138 25OEIS: A045873 3−5OEIS: A015523OEIS: A072263 3−4OEIS: A015521OEIS: A201455 3−3OEIS: A030195OEIS: A172012 3−2OEIS: A007482OEIS: A206776 3−1OEIS: A006190OEIS: A006497 31OEIS: A001906OEIS: A005248 32OEIS: A000225OEIS: A000051 35OEIS: A190959 4−3OEIS: A015530OEIS: A080042 4−2OEIS: A090017 4−1OEIS: A001076OEIS: A014448 41OEIS: A001353OEIS: A003500 42OEIS: A007070OEIS: A056236 43OEIS: A003462OEIS: A034472 44OEIS: A001787 5−3OEIS: A015536 5−2OEIS: A015535 5−1OEIS: A052918OEIS: A087130 51OEIS: A004254OEIS: A003501 54OEIS: A002450OEIS: A052539 61OEIS: A001109OEIS: A003499

## Applications

* Lucas sequences are used in probabilistic Lucas pseudoprime tests, which are part of the commonly used Baillie–PSW primality test.
* Lucas sequences are used in some primality proof methods, including the Lucas–Lehmer–Riesel test, and the N+1 and hybrid N−1/N+1 methods such as those in Brillhart-Lehmer-Selfridge 1975.
* LUC is a public-key cryptosystem based on Lucas sequences that implements the analogs of ElGamal (LUCELG), Diffie–Hellman (LUCDIF), and RSA (LUCRSA). The encryption of the message in LUC is computed as a term of certain Lucas sequence, instead of using modular exponentiation as in RSA or Diffie–Hellman. However, a paper by Bleichenbacher et al. shows that many of the supposed security advantages of LUC over cryptosystems based on modular exponentiation are either not present, or not as substantial as claimed.

## Software

Sagemath implements $U_{n}$ and $V_{n}$ as lucas_number1 and lucas_number2, respectively.