Jacobsthal and Jacobsthal | A DP based solution to find Jacobsthal and Jacobsthal - Lucas numbers ; Return nth Jacobsthal number . ; base case ; Return nth Jacobsthal - Lucas number . ; base case ; Driven Program
#include <bits/stdc++.h> NEW_LINE 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 ; }