Comments on The Pell Numbers
Post
The Pell Numbers
Introduction
The Pell(no, not Bell) Numbers are a simple, Fibonacci-like sequence, defined by the following relation:
$P_n=\begin{cases}0&\mbox{if }n=0;\\1&\mbox{if }n=1;\\2P_{n-1}+P_{n-2}&\mbox{otherwise.}\end{cases}$
They also have a closed form:
$P_n=\frac{\left(1+\sqrt2\right)^n-\left(1-\sqrt2\right)^n}{2\sqrt2}$
And a matrix multiplication based form, for the daring:
$\begin{pmatrix} P_{n+1} & P_n \\ P_n & P_{n-1} \end{pmatrix} = \begin{pmatrix} 2 & 1 \\ 1 & 0 \end{pmatrix}^n.$
Challenge
Your mission, should you choose to accept it, is to do any one of the following:
-
Given $n$, calculate the $n^{th}$ term of the sequence (0 or 1-indexed).
-
Given $n$, calculate the first $n$ elements of the sequence.
-
Output the sequence indefinitely.
Scoring
This is code-golf. Shortest answer in each language wins.
[APL (Dyalog Classic)], 20 18 …
3y ago
[Python 3], 39 bytes …
3y ago
J, 28 bytes ``` :}.@{.m&(+ …
2y ago
Japt, 11 9 bytes Outputs th …
3y ago
[Haskell], 21 bytes …
3y ago
J, 30 char ``` ((2%:2)%(1+ …
2y ago
C (gcc), 35 bytes ``` C f( …
2y ago
Lua 5.4.4, 51 bytes ``` lua …
2y ago
JavaScript, 26 bytes Output …
3y ago
1 comment thread