Pell Number
Last Updated :
18 Aug, 2022
Pell numbers are numbers that are similar to the Fibonacci numbers and are generated by the below formula as follows:
Pn = 2*Pn-1 + Pn-2
with seeds P0 = 0 and P1 = 1
First few Pell numbers are 0, 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378, 5741, 13860, 33461, …. Write a function int pell(int n) that returns Pn.
Examples:
Input : n = 4
Output :12
Input : n = 7
Output : 169
Method 1 (Using Recursion)
C++
#include <bits/stdc++.h>
using namespace std;
int pell( int n)
{
if (n <= 2)
return n;
return 2 * pell(n - 1) + pell(n - 2);
}
int main()
{
int n = 4;
cout << " " << pell(n);
return 0;
}
|
C
#include <stdio.h>
int pell( int n)
{
if (n <= 2)
return n;
return 2 * pell(n - 1) + pell(n - 2);
}
int main()
{
int n = 4;
printf ( "%d" , pell(n));
return 0;
}
|
Java
class PellNumber {
public static int pell( int n)
{
if (n <= 2 )
return n;
return 2 * pell(n - 1 ) + pell(n - 2 );
}
public static void main(String args[])
{
int n = 4 ;
System.out.println(pell(n));
}
}
|
Python3
def pell(n) :
if (n < = 2 ) :
return n
return ( 2 * pell(n - 1 ) + pell(n - 2 ))
n = 4 ;
print (pell(n))
|
C#
using System;
class PellNumber {
public static int pell( int n)
{
if (n <= 2)
return n;
return 2 * pell(n - 1) + pell(n - 2);
}
public static void Main()
{
int n = 4;
Console.Write(pell(n));
}
}
|
PHP
<?php
function pell( $n )
{
if ( $n <= 2)
return $n ;
return 2 * pell( $n - 1) +
pell( $n - 2);
}
$n = 4;
echo (pell( $n ));
?>
|
Javascript
<script>
function pell(n)
{
if (n <= 2)
return n;
return 2 * pell(n - 1) +
pell(n - 2);
}
let n = 4;
document.write(pell(n));
</script>
|
Time Complexity: O(2n) i.e exponential time complexity.
Auxiliary Space: O(n)
Method 2 (Iterative)
C++
#include <bits/stdc++.h>
using namespace std;
int pell( int n)
{
if (n <= 2)
return n;
int a = 1;
int b = 2;
int c, i;
for (i = 3; i <= n; i++) {
c = 2 * b + a;
a = b;
b = c;
}
return b;
}
int main()
{
int n = 4;
cout << pell(n);
return 0;
}
|
C
#include <stdio.h>
int pell( int n)
{
if (n <= 2)
return n;
int a = 1;
int b = 2;
int c, i;
for (i = 3; i <= n; i++) {
c = 2 * b + a;
a = b;
b = c;
}
return b;
}
int main()
{
int n = 4;
printf ( "%d" , pell(n));
return 0;
}
|
Java
class PellNumber {
public static int pell( int n)
{
if (n <= 2 )
return n;
int a = 1 ;
int b = 2 ;
int c;
for ( int i = 3 ; i <= n; i++) {
c = 2 * b + a;
a = b;
b = c;
}
return b;
}
public static void main(String args[])
{
int n = 4 ;
System.out.println(pell(n));
}
}
|
Python
def pell(n) :
if (n < = 2 ) :
return n
a = 1
b = 2
for i in range ( 3 , n + 1 ) :
c = 2 * b + a
a = b
b = c
return b
n = 4
print (pell(n))
|
C#
using System;
class PellNumber {
public static int pell( int n)
{
if (n <= 2)
return n;
int a = 1;
int b = 2;
int c;
for ( int i = 3; i <= n; i++) {
c = 2 * b + a;
a = b;
b = c;
}
return b;
}
public static void Main()
{
int n = 4;
Console.Write(pell(n));
}
}
|
PHP
<?php
function pell( $n )
{
if ( $n <= 2)
return $n ;
$a = 1;
$b = 2;
$c ; $i ;
for ( $i = 3; $i <= $n ; $i ++)
{
$c = 2 * $b + $a ;
$a = $b ;
$b = $c ;
}
return $b ;
}
$n = 4;
echo (pell( $n ));
?>
|
Javascript
<script>
function pell(n)
{
if (n <= 2)
return n;
let a = 1;
let b = 2;
let c;
for (let i = 3; i <= n; i++) {
c = 2 * b + a;
a = b;
b = c;
}
return b;
}
let n = 4;
document.write(pell(n));
</script>
|
Output:
12
Time Complexity: O(n)
Auxiliary Space: O(1)
Using matrix calculation:
This another O(n) that relies on the fact that if we n times multiply the matrix M = {{2, 1}, {1, 0}} to itself (in other words calculate power(M, n)), then we get the (n+1)th Pell number as the element at row and column (0, 0) in the resultant matrix.
Time Complexity: O(log n) Since we can compute n-th power of a 2 × 2 matrix in O(log n) times
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...