📜  Jacobsthal 和 Jacobsthal-Lucas 数

📅  最后修改于: 2021-09-22 09:57:10             🧑  作者: Mango

Jacobsthal 数列是类似于斐波那契数列的加法数列,由递推关系 J n = J n-1 + 2J n-2 定义,初始项 J 0 = 0 和 J 1 = 1。序列中的数字是称为Jacobsthal 数。它们是特定类型的卢卡斯序列 U n (P, Q),其中 P = 1 且 Q = -2。

第一个 Jacobsthal 数是:
0, 1, 1, 3, 5, 11, 21, 43, 85, 171, 341, 683, 1365, 2731, 5461, 10923, 21845, 43691, ……
Jacobsthal 数由递推关系定义:
{\displaystyle J_{n}=\left\{\begin{matrix} 0 & & if n=0\\1&&ifn=1 \\ J_{n-1} + 2J_{n-2}&&ifn>1 \end{matrix}\right.}

雅各布斯塔尔-卢卡斯数
Jacobsthal-Lucas 数表示互补的 Lucas 序列 V n (1, -2)。它们满足与 Jacobsthal 数相同的递推关系,但具有不同的初始值: {\displaystyle L_{n}=\left\{\begin{matrix} 2 & & if n=0\\1&&ifn=1 \\ L_{n-1} + 2L_{n-2}&&ifn>1 \end{matrix}\right.}
给定一个正整数n 。任务是找到第 n 个 Jacobsthal 和 Jacobsthal-Lucas 数。

例子 :

Input : n = 5
Output :
Jacobsthal number: 11
Jacobsthal-Lucas number: 31

Input : n = 4
Output :
Jacobsthal number: 5
Jacobsthal-Lucas number: 17

下面是使用递归查找第 n 个 Jacobsthal 和 Jacobsthal-Lucas 数的实现。

C++
// A simple C++ recursive solution to find
// Jacobsthal and Jacobsthal-Lucas numbers
#include 
using namespace std;
 
// Return nth Jacobsthal number.
int Jacobsthal(int n)
{
    // base case
    if (n == 0)
        return 0;
 
    // base case
    if (n == 1)
        return 1;
 
    // recursive step.
    return Jacobsthal(n - 1) + 2 * Jacobsthal(n - 2);
}
 
// Return nth Jacobsthal-Lucas number.
int Jacobsthal_Lucas(int n)
{
    // base case
    if (n == 0)
        return 2;
 
    // base case
    if (n == 1)
        return 1;
 
    // recursive step.
    return Jacobsthal_Lucas(n - 1) +
           2 * Jacobsthal_Lucas(n - 2);
}
 
// Driven Program
int main()
{
    int n = 5;
    cout << "Jacobsthal number: " << Jacobsthal(n) << endl;
    cout << "Jacobsthal-Lucas number: " << Jacobsthal_Lucas(n) << endl;
    return 0;
}


Java
// A simple recursive solution
// to find Jacobsthal and
// Jacobsthal-Lucas numbers
import java.util.*;
import java.lang.*;
 
public class GfG{
 
    // Return nth Jacobsthal number.
    public static int Jacobsthal(int n)
    {
        // base case
        if (n == 0)
            return 0;
 
        // base case
        if (n == 1)
            return 1;
 
        // recursive step.
        return Jacobsthal(n - 1) + 2 * Jacobsthal(n - 2);
    }
 
    // Return nth Jacobsthal-Lucas number.
    public static int Jacobsthal_Lucas(int n)
    {
        // base case
        if (n == 0)
            return 2;
 
        // base case
        if (n == 1)
            return 1;
 
        // recursive step.
        return Jacobsthal_Lucas(n - 1) +
               2 * Jacobsthal_Lucas(n - 2);
    }
 
    // Driver function
    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));
    }
}
 
/* This code is contributed Sagar Shukla */


Python3
# A simple Python3 recursive solution to 
# find Jacobsthal and Jacobsthal-Lucas
# numbers
 
# Return nth Jacobsthal number.
def Jacobsthal(n):
    # base case
    if (n == 0):
        return 0
 
    # base case
    if (n == 1):
        return 1
 
    # recursive step.
    return Jacobsthal(n - 1) + 2 * Jacobsthal(n - 2)
 
# Return nth Jacobsthal-Lucas number.
def Jacobsthal_Lucas(n):
    # base case
    if (n == 0):
        return 2
         
    # base case
    if (n == 1):
        return 1
 
    # recursive step.
    return Jacobsthal_Lucas(n - 1) + 2 * Jacobsthal_Lucas(n - 2)
 
# Driven Program
n = 5
print("Jacobsthal number:", Jacobsthal(n))
print("Jacobsthal-Lucas number:", Jacobsthal_Lucas(n))
 
# This code is contributed by Smitha Dinesh Semwal


C#
// A simple recursive solution
// to find Jacobsthal and
// Jacobsthal-Lucas numbers
using System;
 
public class GfG {
 
    // Return nth Jacobsthal number.
    public static int Jacobsthal(int n)
    {
        // base case
        if (n == 0) return 0;
 
        // base case
        if (n == 1) return 1;
 
        // recursive step.
        return Jacobsthal(n - 1) +
               2 * Jacobsthal(n - 2);
    }
 
    // Return nth Jacobsthal-Lucas number.
    public static int Jacobsthal_Lucas(int n)
    {
        // base case
        if (n == 0) return 2;
 
        // base case
        if (n == 1) return 1;
 
        // recursive step
        return Jacobsthal_Lucas(n - 1) +
                2 * Jacobsthal_Lucas(n - 2);
    }
 
    // Driver function
    public static void Main() {
        int n = 5;
        Console.WriteLine("Jacobsthal number: "
                                + Jacobsthal(n));
        Console.WriteLine("Jacobsthal-Lucas number: "
                                + Jacobsthal_Lucas(n));
    }
}
 
// This code is contributed vt_m


PHP


Javascript


C++
// A DP based solution to find Jacobsthal
// and Jacobsthal-Lucas numbers
#include 
using namespace std;
 
// Return nth Jacobsthal number.
int Jacobsthal(int n)
{
    int dp[n + 1];
 
    // base case
    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];
}
 
// Return nth Jacobsthal-Lucas number.
int Jacobsthal_Lucas(int n)
{
    int dp[n + 1];
 
    // base case
    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];
}
// Driven Program
int main()
{
    int n = 5;
    cout << "Jacobsthal number: " << Jacobsthal(n) << endl;
    cout << "Jacobsthal-Lucas number: " << Jacobsthal_Lucas(n) << endl;
    return 0;
}


Java
// A DP based solution
// to find Jacobsthal and
// Jacobsthal-Lucas numbers
import java.util.*;
import java.lang.*;
 
public class GfG{
 
    // Return nth Jacobsthal number.
    public static int Jacobsthal(int n)
    {
        int[] dp = new int[n + 1];
 
        // base case
        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];
    }
 
    // Return nth Jacobsthal-Lucas number.
    public static int Jacobsthal_Lucas(int n)
    {
        int[] dp = new int[n + 1];
 
        // base case
        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];
    }
 
    // Driver function
    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));
    }
     
}
 
/* This code is contributed Sagar Shukla */


Python3
# A DP based solution to find
# Jacobsthal and Jacobsthal-
# Lucas numbers
 
# Return nth Jacobsthal number.
def Jacobsthal(n):
    dp = [0] * (n + 1)
 
    # base case
    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]
 
 
# Return nth Jacobsthal-
# Lucas number.
def Jacobsthal_Lucas(n):
 
    dp=[0] * (n + 1)
     
    # base case
    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]
 
# Driven Program
n = 5
 
print("Jacobsthal number:",Jacobsthal(n))
print("Jacobsthal-Lucas number:",Jacobsthal_Lucas(n))
 
# This code is contributed by Smitha Dinesh Semwal


C#
// A DP based solution
// to find Jacobsthal and
// Jacobsthal-Lucas numbers
using System;
 
public class GfG {
 
    // Return nth Jacobsthal number.
    public static int Jacobsthal(int n)
    {
        int[] dp = new int[n + 1];
 
        // base case
        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];
    }
 
    // Return nth Jacobsthal-Lucas number.
    public static int Jacobsthal_Lucas(int n)
    {
        int[] dp = new int[n + 1];
 
        // base case
        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];
    }
 
    // Driver Code
    public static void Main() {
        int n = 5;
        Console.WriteLine("Jacobsthal number: "
                                + Jacobsthal(n));
        Console.WriteLine("Jacobsthal-Lucas number: "
                                + Jacobsthal_Lucas(n));
    }
     
}
 
// This code is contributed vt_m


PHP


Javascript


输出 :

Jacobsthal number: 11
Jacobsthal-Lucas number: 31

下面是使用动态规划查找第 n 个 Jacobsthal 和 Jacobsthal-Lucas 数的实现。

C++

// A DP based solution to find Jacobsthal
// and Jacobsthal-Lucas numbers
#include 
using namespace std;
 
// Return nth Jacobsthal number.
int Jacobsthal(int n)
{
    int dp[n + 1];
 
    // base case
    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];
}
 
// Return nth Jacobsthal-Lucas number.
int Jacobsthal_Lucas(int n)
{
    int dp[n + 1];
 
    // base case
    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];
}
// Driven Program
int main()
{
    int n = 5;
    cout << "Jacobsthal number: " << Jacobsthal(n) << endl;
    cout << "Jacobsthal-Lucas number: " << Jacobsthal_Lucas(n) << endl;
    return 0;
}

Java

// A DP based solution
// to find Jacobsthal and
// Jacobsthal-Lucas numbers
import java.util.*;
import java.lang.*;
 
public class GfG{
 
    // Return nth Jacobsthal number.
    public static int Jacobsthal(int n)
    {
        int[] dp = new int[n + 1];
 
        // base case
        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];
    }
 
    // Return nth Jacobsthal-Lucas number.
    public static int Jacobsthal_Lucas(int n)
    {
        int[] dp = new int[n + 1];
 
        // base case
        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];
    }
 
    // Driver function
    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));
    }
     
}
 
/* This code is contributed Sagar Shukla */

蟒蛇3

# A DP based solution to find
# Jacobsthal and Jacobsthal-
# Lucas numbers
 
# Return nth Jacobsthal number.
def Jacobsthal(n):
    dp = [0] * (n + 1)
 
    # base case
    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]
 
 
# Return nth Jacobsthal-
# Lucas number.
def Jacobsthal_Lucas(n):
 
    dp=[0] * (n + 1)
     
    # base case
    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]
 
# Driven Program
n = 5
 
print("Jacobsthal number:",Jacobsthal(n))
print("Jacobsthal-Lucas number:",Jacobsthal_Lucas(n))
 
# This code is contributed by Smitha Dinesh Semwal

C#

// A DP based solution
// to find Jacobsthal and
// Jacobsthal-Lucas numbers
using System;
 
public class GfG {
 
    // Return nth Jacobsthal number.
    public static int Jacobsthal(int n)
    {
        int[] dp = new int[n + 1];
 
        // base case
        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];
    }
 
    // Return nth Jacobsthal-Lucas number.
    public static int Jacobsthal_Lucas(int n)
    {
        int[] dp = new int[n + 1];
 
        // base case
        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];
    }
 
    // Driver Code
    public static void Main() {
        int n = 5;
        Console.WriteLine("Jacobsthal number: "
                                + Jacobsthal(n));
        Console.WriteLine("Jacobsthal-Lucas number: "
                                + Jacobsthal_Lucas(n));
    }
     
}
 
// This code is contributed vt_m

PHP


Javascript


输出:

Jacobsthal number: 11
Jacobsthal-Lucas number: 31

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程