Jacobsthal and Jacobsthal | Return nth Jacobsthal number . ; Base case ; Return nth Jacobsthal - Lucas number . ; Base case ; Driver code
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 ( n ) + " " ) ; document . write ( " " + Jacobsthal_Lucas ( n ) ) ;