Jacobsthal and Jacobsthal-Lucas numbers
Last Updated :
07 Feb, 2024
Jacobsthal numbers
The Jacobsthal numbers are the numbers obtained by the Uns in the Lucas sequence with P=1 and Q=-2, corresponding to a = 2 and b = -1.
Jacobsthal numbers are defined by the recurrence relation:
The first Jacobsthal numbers are:
0, 1, 1, 3, 5, 11, 21, 43, 85, 171, 341, 683, 1365, 2731, 5461, 10923, 21845, 43691, ……
Jacobsthal-Lucas numbers
Jacobsthal-Lucas numbers represent the complementary Lucas sequence Vn(1, -2). They satisfy the same recurrence relation as Jacobsthal numbers but have different initial values:
Given a positive integer n, the task is to find nth Jacobsthal and Jacobsthal-Lucas numbers.
Examples :
Input : n = 5
Output :
Jacobsthal number: 11
Jacobsthal-Lucas number: 31
Input : n = 4
Output :
Jacobsthal number: 5
Jacobsthal-Lucas number: 17
Below is the implementation of finding nth Jacobsthal and Jacobsthal-Lucas numbers using recursion.
C++
#include <bits/stdc++.h>
using namespace std;
int Jacobsthal( int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
return Jacobsthal(n - 1) + 2 * Jacobsthal(n - 2);
}
int Jacobsthal_Lucas( int n)
{
if (n == 0)
return 2;
if (n == 1)
return 1;
return Jacobsthal_Lucas(n - 1)
+ 2 * Jacobsthal_Lucas(n - 2);
}
int main()
{
int n = 5;
cout << "Jacobsthal number: " << Jacobsthal(n) << endl;
cout << "Jacobsthal-Lucas number: "
<< Jacobsthal_Lucas(n) << endl;
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
public class GfG {
public static int Jacobsthal( int n)
{
if (n == 0 )
return 0 ;
if (n == 1 )
return 1 ;
return Jacobsthal(n - 1 ) + 2 * Jacobsthal(n - 2 );
}
public static int Jacobsthal_Lucas( int n)
{
if (n == 0 )
return 2 ;
if (n == 1 )
return 1 ;
return Jacobsthal_Lucas(n - 1 )
+ 2 * Jacobsthal_Lucas(n - 2 );
}
public static void main(String argc[])
{
int n = 5 ;
System.out.println( "Jacobsthal number: "
+ Jacobsthal(n));
System.out.println( "Jacobsthal-Lucas number: "
+ Jacobsthal_Lucas(n));
}
}
|
Python3
def Jacobsthal(n):
if (n = = 0 ):
return 0
if (n = = 1 ):
return 1
return Jacobsthal(n - 1 ) + 2 * Jacobsthal(n - 2 )
def Jacobsthal_Lucas(n):
if (n = = 0 ):
return 2
if (n = = 1 ):
return 1
return Jacobsthal_Lucas(n - 1 ) + 2 * Jacobsthal_Lucas(n - 2 )
n = 5
print ( "Jacobsthal number:" , Jacobsthal(n))
print ( "Jacobsthal-Lucas number:" , Jacobsthal_Lucas(n))
|
C#
using System;
public class GfG {
public static int Jacobsthal( int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
return Jacobsthal(n - 1) + 2 * Jacobsthal(n - 2);
}
public static int Jacobsthal_Lucas( int n)
{
if (n == 0)
return 2;
if (n == 1)
return 1;
return Jacobsthal_Lucas(n - 1)
+ 2 * Jacobsthal_Lucas(n - 2);
}
public static void Main()
{
int n = 5;
Console.WriteLine( "Jacobsthal number: "
+ Jacobsthal(n));
Console.WriteLine( "Jacobsthal-Lucas number: "
+ Jacobsthal_Lucas(n));
}
}
|
Javascript
<script>
function Jacobsthal(n)
{
let dp = [];
dp[0] = 0;
dp[1] = 1;
for (let i = 2; i <= n; i++)
dp[i] = dp[i - 1] + 2 * dp[i - 2];
return dp[n];
}
function Jacobsthal_Lucas(n)
{
let dp = [];
dp[0] = 2;
dp[1] = 1;
for (let i = 2; i <= n; i++)
dp[i] = dp[i - 1] + 2 * dp[i - 2];
return dp[n];
}
let n = 5;
document.write( "Jacobsthal number: "
+ Jacobsthal(n) + "<br/>" );
document.write( "Jacobsthal-Lucas number: "
+ Jacobsthal_Lucas(n));
</script>
|
PHP
<?php
function Jacobsthal( $n )
{
if ( $n == 0)
return 0;
if ( $n == 1)
return 1;
return Jacobsthal( $n - 1) +
2 * Jacobsthal( $n - 2);
}
function Jacobsthal_Lucas( $n )
{
if ( $n == 0)
return 2;
if ( $n == 1)
return 1;
return Jacobsthal_Lucas( $n - 1) +
2 * Jacobsthal_Lucas( $n - 2);
}
$n = 5;
echo "Jacobsthal number: " ,
Jacobsthal( $n ) , "\n" ;
echo "Jacobsthal-Lucas number: " ,
Jacobsthal_Lucas( $n ), "\n" ;
?>
|
OutputJacobsthal number: 11
Jacobsthal-Lucas number: 31
Time Complexity: O(2^n), Where n is the given number
Auxiliary Space: O(1)
Below is the implementation of finding nth Jacobsthal and Jacobsthal-Lucas numbers using Dynamic Programming.
C++
#include <bits/stdc++.h>
using namespace std;
int Jacobsthal( int n)
{
int dp[n + 1];
dp[0] = 0;
dp[1] = 1;
for ( int i = 2; i <= n; i++)
dp[i] = dp[i - 1] + 2 * dp[i - 2];
return dp[n];
}
int Jacobsthal_Lucas( int n)
{
int dp[n + 1];
dp[0] = 2;
dp[1] = 1;
for ( int i = 2; i <= n; i++)
dp[i] = dp[i - 1] + 2 * dp[i - 2];
return dp[n];
}
int main()
{
int n = 5;
cout << "Jacobsthal number: " << Jacobsthal(n) << endl;
cout << "Jacobsthal-Lucas number: "
<< Jacobsthal_Lucas(n) << endl;
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
public class GfG {
public static int Jacobsthal( int n)
{
int [] dp = new int [n + 1 ];
dp[ 0 ] = 0 ;
dp[ 1 ] = 1 ;
for ( int i = 2 ; i <= n; i++)
dp[i] = dp[i - 1 ] + 2 * dp[i - 2 ];
return dp[n];
}
public static int Jacobsthal_Lucas( int n)
{
int [] dp = new int [n + 1 ];
dp[ 0 ] = 2 ;
dp[ 1 ] = 1 ;
for ( int i = 2 ; i <= n; i++)
dp[i] = dp[i - 1 ] + 2 * dp[i - 2 ];
return dp[n];
}
public static void main(String argc[])
{
int n = 5 ;
System.out.println( "Jacobsthal number: "
+ Jacobsthal(n));
System.out.println( "Jacobsthal-Lucas number: "
+ Jacobsthal_Lucas(n));
}
}
|
Python3
def Jacobsthal(n):
dp = [ 0 ] * (n + 1 )
dp[ 0 ] = 0
dp[ 1 ] = 1
for i in range ( 2 , n + 1 ):
dp[i] = dp[i - 1 ] + 2 * dp[i - 2 ]
return dp[n]
def Jacobsthal_Lucas(n):
dp = [ 0 ] * (n + 1 )
dp[ 0 ] = 2
dp[ 1 ] = 1
for i in range ( 2 , n + 1 ):
dp[i] = dp[i - 1 ] + 2 * dp[i - 2 ]
return dp[n]
n = 5
print ( "Jacobsthal number:" , Jacobsthal(n))
print ( "Jacobsthal-Lucas number:" , Jacobsthal_Lucas(n))
|
C#
using System;
public class GfG {
public static int Jacobsthal( int n)
{
int [] dp = new int [n + 1];
dp[0] = 0;
dp[1] = 1;
for ( int i = 2; i <= n; i++)
dp[i] = dp[i - 1] + 2 * dp[i - 2];
return dp[n];
}
public static int Jacobsthal_Lucas( int n)
{
int [] dp = new int [n + 1];
dp[0] = 2;
dp[1] = 1;
for ( int i = 2; i <= n; i++)
dp[i] = dp[i - 1] + 2 * dp[i - 2];
return dp[n];
}
public static void Main()
{
int n = 5;
Console.WriteLine( "Jacobsthal number: "
+ Jacobsthal(n));
Console.WriteLine( "Jacobsthal-Lucas number: "
+ Jacobsthal_Lucas(n));
}
}
|
Javascript
<script>
function Jacobsthal(n)
{
let dp = new Array(n + 1);
dp[0] = 0;
dp[1] = 1;
for (let i = 2; i <= n; i++)
dp[i] = dp[i - 1] + 2 * dp[i - 2];
return dp[n];
}
function Jacobsthal_Lucas(n)
{
let dp = new Array(n + 1);
dp[0] = 2;
dp[1] = 1;
for (let i = 2; i <= n; i++)
dp[i] = dp[i - 1] + 2 * dp[i - 2];
return dp[n];
}
let n = 5;
document.write( "Jacobsthal number: " +
Jacobsthal(n) + "<br>" );
document.write( "Jacobsthal-Lucas number: " +
Jacobsthal_Lucas(n));
</script>
|
PHP
<?php
function Jacobsthal( $n )
{
$dp [0] = 0;
$dp [1] = 1;
for ( $i = 2; $i <= $n ; $i ++)
$dp [ $i ] = $dp [ $i - 1] + 2 *
$dp [ $i - 2];
return $dp [ $n ];
}
function Jacobsthal_Lucas( $n )
{
$dp [0] = 2;
$dp [1] = 1;
for ( $i = 2; $i <= $n ; $i ++)
$dp [ $i ] = $dp [ $i - 1] + 2 *
$dp [ $i - 2];
return $dp [ $n ];
}
$n = 5;
echo "Jacobsthal number: " ,
Jacobsthal( $n ), "\n" ;
echo "Jacobsthal-Lucas number: " ,
Jacobsthal_Lucas( $n ), "\n" ;
?>
|
OutputJacobsthal number: 11
Jacobsthal-Lucas number: 31
Time Complexity: O(n), Where n is the given number
Auxiliary Space: O(n)
Efficient approach : Space optimization O(1)
In previous approach the current value dp[i] is only depend only the previous 2 values dp[i-1] and dp[i-2]. So to optimize the space complexity we can store these values in 2 variables prev1 and prev2.
Implementation Steps:
- In both the functions we use variables prev1 and prev2 to store previous values.
- Create curr to store current value.
- Iterate over subproblems and get current value from previous values.
- At last return and print the final answer.
Implementation:
C++
#include <iostream>
int Jacobsthal( int n) {
int prev1 = 0, prev2 = 1;
if (n == 0) {
return prev1;
}
if (n == 1) {
return prev2;
}
for ( int i = 2; i <= n; ++i) {
int curr = prev2 + 2 * prev1;
prev1 = prev2;
prev2 = curr;
}
return prev2;
}
int Jacobsthal_Lucas( int n) {
int prev1 = 2, prev2 = 1;
if (n == 0) {
return prev1;
}
if (n == 1) {
return prev2;
}
for ( int i = 2; i <= n; ++i) {
int curr = prev2 + 2 * prev1;
prev1 = prev2;
prev2 = curr;
}
return prev2;
}
int main() {
int n = 5;
std::cout << "Jacobsthal number: " << Jacobsthal(n) << std::endl;
std::cout << "Jacobsthal-Lucas number: " << Jacobsthal_Lucas(n) << std::endl;
return 0;
}
|
C
#include <bits/stdc++.h>
using namespace std;
int Jacobsthal( int n)
{
int curr;
int prev1 , prev2;
prev1 = 0;
prev2 = 1;
for ( int i = 2; i <= n; i++){
curr = prev2 + 2 * prev1;
prev1=prev2;
prev2=curr;
}
return curr;
}
int Jacobsthal_Lucas( int n)
{
int curr;
int prev1 , prev2;
prev1 = 2;
prev2 = 1;
for ( int i = 2; i <= n; i++){
curr = prev2 + 2 * prev1;
prev1= prev2;
prev2=curr;
}
return curr;
}
int main()
{
int n = 5;
cout << "Jacobsthal number: " << Jacobsthal(n) << endl;
cout << "Jacobsthal-Lucas number: "
<< Jacobsthal_Lucas(n) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int Jacobsthal( int n)
{
int curr;
int prev1, prev2;
if (n < 2 ) {
return n;
}
prev1 = 0 ;
prev2 = 1 ;
curr = 1 ;
for ( int i = 2 ; i <= n; i++) {
curr = prev2 + 2 * prev1;
prev1 = prev2;
prev2 = curr;
}
return curr;
}
public static int Jacobsthal_Lucas( int n)
{
int curr;
int prev1, prev2;
if (n < 2 ) {
return n + 1 ;
}
prev1 = 2 ;
prev2 = 1 ;
curr = 1 ;
for ( int i = 2 ; i <= n; i++) {
curr = prev2 + 2 * prev1;
prev1 = prev2;
prev2 = curr;
}
return curr;
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println( "Jacobsthal number: "
+ Jacobsthal(n));
System.out.println( "Jacobsthal-Lucas number: "
+ Jacobsthal_Lucas(n));
}
}
|
Python3
def Jacobsthal(n):
prev1, prev2 = 0 , 1
if n = = 0 :
return prev1
if n = = 1 :
return prev2
for i in range ( 2 , n + 1 ):
curr = prev2 + 2 * prev1
prev1, prev2 = prev2, curr
return curr
def Jacobsthal_Lucas(n):
prev1, prev2 = 2 , 1
if n = = 0 :
return prev1
if n = = 1 :
return prev2
for i in range ( 2 , n + 1 ):
curr = prev2 + 2 * prev1
prev1, prev2 = prev2, curr
return curr
if __name__ = = '__main__' :
n = 5
print ( "Jacobsthal number:" , Jacobsthal(n))
print ( "Jacobsthal-Lucas number:" , Jacobsthal_Lucas(n))
|
C#
using System;
class Program
{
static int Jacobsthal( int n)
{
int prev1 = 0, prev2 = 1;
if (n == 0)
{
return prev1;
}
if (n == 1)
{
return prev2;
}
for ( int i = 2; i <= n; ++i)
{
int curr = prev2 + 2 * prev1;
prev1 = prev2;
prev2 = curr;
}
return prev2;
}
static int Jacobsthal_Lucas( int n)
{
int prev1 = 2, prev2 = 1;
if (n == 0)
{
return prev1;
}
if (n == 1)
{
return prev2;
}
for ( int i = 2; i <= n; ++i)
{
int curr = prev2 + 2 * prev1;
prev1 = prev2;
prev2 = curr;
}
return prev2;
}
static void Main()
{
int n = 5;
Console.WriteLine($ "Jacobsthal number: {Jacobsthal(n)}" );
Console.WriteLine($ "Jacobsthal-Lucas number: {Jacobsthal_Lucas(n)}" );
}
}
|
Javascript
<script>
function Jacobsthal(n) {
let curr, prev1 = 0, prev2 = 1;
for (let i = 2; i <= n; i++){
curr = prev2 + 2 * prev1;
prev1 = prev2;
prev2 = curr;
}
return curr;
}
function Jacobsthal_Lucas(n) {
let curr, prev1 = 2, prev2 = 1;
for (let i = 2; i <= n; i++){
curr = prev2 + 2 * prev1;
prev1 = prev2;
prev2 = curr;
}
return curr;
}
const n = 5;
console.log(`Jacobsthal number: ${Jacobsthal(n)}`);
console.log(`Jacobsthal-Lucas number: ${Jacobsthal_Lucas(n)}`);
</script>
|
OutputJacobsthal number: 11
Jacobsthal-Lucas number: 31
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...