📜  四重数

📅  最后修改于: 2021-09-17 06:59:42             🧑  作者: Mango

四联数是由递推关系定义的斐波那契数的推广

对于 n>=4。它们代表斐波那契 n 步数的 n=4 情况。 n=0, 1, … 的前几项是 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, …
给定一个数字 N。任务是找到第 N 个四连数。

例子

Input: 5
Output: 4

Input: 9
Output: 108 

推荐:在继续解决方案之前,请先在{IDE}上尝试您的方法。

一种幼稚的方法是遵循递归来找到数字并使用递归来解决它。
下面是上述方法的实现。

C++
// A simple recursive CPP program to print
// the nth tetranacci numbers.
#include 
using namespace std;
 
// Function to return the
// N-th tetranacci number
int printTetraRec(int n)
{
    // base cases
    if (n == 0)
        return 0;
    // base cases
    if (n == 1 || n == 2)
        return 1;
    // base cases
    if (n == 3)
        return 2;
 
    else
        return printTetraRec(n - 1) + printTetraRec(n - 2)
               + printTetraRec(n - 3) + printTetraRec(n - 4);
}
 
// function to print the nth tetranacci number
void printTetra(int n)
{
    cout << printTetraRec(n) << " ";
}
 
// Driver code
int main()
{
    int n = 10;
    printTetra(n);
    return 0;
}


Java
// A simple recursive Java
// program to print the nth
// tetranacci numbers.
class GFG
{
// Function to return the
// N-th tetranacci number
static int printTetraRec(int n)
{
    // base cases
    if (n == 0)
        return 0;
    // base cases
    if (n == 1 || n == 2)
        return 1;
    // base cases
    if (n == 3)
        return 2;
 
    else
        return printTetraRec(n - 1) +
               printTetraRec(n - 2) +
               printTetraRec(n - 3) +
               printTetraRec(n - 4);
}
 
// function to print the
// Nth tetranacci number
static void printTetra(int n)
{
    System.out.println(printTetraRec(n) + " ");
}
 
// Driver code
public static void main(String[] args)
{
    int n = 10;
    printTetra(n);
}
}
 
// This code is contributed by mits


Python3
# A simple recursive Python3 program
# to print the nth tetranacci numbers.
 
# Function to return the
# N-th tetranacci number
def printTetraRec(n):
     
    # base cases
    if (n == 0):
        return 0;
         
    # base cases
    if (n == 1 or n == 2):
        return 1;
         
    # base cases
    if (n == 3):
        return 2;
 
    else:
        return (printTetraRec(n - 1) +
                printTetraRec(n - 2) +
                printTetraRec(n - 3) +
                printTetraRec(n - 4));
 
# function to print the
# nth tetranacci number
def printTetra(n):
    print(printTetraRec(n), end = " ");
 
# Driver code
n = 10;
printTetra(n);
 
# This code is contributed
# by mits


C#
// A simple recursive C#
// program to print the nth
// tetranacci numbers.
class GFG
{
     
// Function to return the
// N-th tetranacci number
static int printTetraRec(int n)
{
    // base cases
    if (n == 0)
        return 0;
    // base cases
    if (n == 1 || n == 2)
        return 1;
    // base cases
    if (n == 3)
        return 2;
 
    else
        return printTetraRec(n - 1) +
               printTetraRec(n - 2) +
               printTetraRec(n - 3) +
               printTetraRec(n - 4);
}
 
// function to print the
// Nth tetranacci number
static void printTetra(int n)
{
    System.Console.WriteLine(
           printTetraRec(n) + " ");
}
 
// Driver code
static void Main()
{
    int n = 10;
    printTetra(n);
}
}
 
// This code is contributed by mits


PHP


Javascript


C++
// A DP based CPP
// program to print
// the nth tetranacci number
#include 
using namespace std;
 
// Function to print the
// N-th tetranacci number
int printTetra(int n)
{
    int dp[n + 5];
    // base cases
    dp[0] = 0;
    dp[1] = dp[2] = 1;
    dp[3] = 2;
 
    for (int i = 4; i <= n; i++)
        dp[i] = dp[i - 1] + dp[i - 2] +
                dp[i - 3] + dp[i - 4];
 
    cout << dp[n];
}
 
// Driver code
int main()
{
    int n = 10;
    printTetra(n);
    return 0;
}


Java
// A DP based Java
// program to print
// the nth tetranacci number
 
class GFG{
// Function to print the
// N-th tetranacci number
static void printTetra(int n)
{
    int[] dp=new int[n + 5];
    // base cases
    dp[0] = 0;
    dp[1] = dp[2] = 1;
    dp[3] = 2;
 
    for (int i = 4; i <= n; i++)
        dp[i] = dp[i - 1] + dp[i - 2] +
                dp[i - 3] + dp[i - 4];
 
    System.out.print(dp[n]);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 10;
    printTetra(n);
}
}
// This code is contributed by mits


Python3
# A DP based Python3 program to print
# the nth tetranacci number
 
# Function to print the
# N-th tetranacci number
def printTetra(n):
    dp = [0] * (n + 5);
     
    # base cases
    dp[0] = 0;
    dp[1] = 1;
    dp[2] = 1;
    dp[3] = 2;
 
    for i in range(4, n + 1):
        dp[i] = (dp[i - 1] + dp[i - 2] +
                 dp[i - 3] + dp[i - 4]);
 
    print(dp[n]);
 
# Driver code
n = 10;
printTetra(n);
 
# This code is contributed by mits


C#
// A DP based C#
// program to print
// the nth tetranacci number
 
class GFG{
// Function to print the
// N-th tetranacci number
static void printTetra(int n)
{
    int[] dp=new int[n + 5];
    // base cases
    dp[0] = 0;
    dp[1] = dp[2] = 1;
    dp[3] = 2;
 
    for (int i = 4; i <= n; i++)
        dp[i] = dp[i - 1] + dp[i - 2] +
                dp[i - 3] + dp[i - 4];
 
    System.Console.WriteLine(dp[n]);
}
 
// Driver code
static void Main()
{
    int n = 10;
    printTetra(n);
}
}
// This code is contributed by mits


PHP


Javascript


C++
// A space optimized
// based CPP program to
// print the nth tetranacci number
#include 
using namespace std;
 
// Function to print the
// N-th tetranacci number
void printTetra(int n)
{
    if (n < 0)
        return;
 
    // Initialize first
    // four numbers to base cases
    int first = 0, second = 1;
    int third = 1, fourth = 2;
 
    // declare a current variable
    int curr;
 
    if (n == 0)
        cout << first;
    else if (n == 1 || n == 2)
        cout << second;
 
    else if (n == 3)
        cout << fourth;
 
    else {
 
        // Loop to add previous
        // four numbers for
        // each number starting
        // from 4 and then assign
        // first, second, third
        // to second, third, fourth and
        // curr to fourth respectively
        for (int i = 4; i <= n; i++) {
            curr = first + second + third + fourth;
            first = second;
            second = third;
            third = fourth;
            fourth = curr;
        }
        cout << curr;
    }
}
 
// Driver code
int main()
{
    int n = 10;
    printTetra(n);
    return 0;
}


Java
// A space optimized
// based Java program to
// print the nth tetranacci number
import java.io.*;
import java.util.*;
import java.lang.*;
 
class GFG{
// Function to print the
// N-th tetranacci number
static void printTetra(int n)
{
    if (n < 0)
        return;
 
    // Initialize first
    // four numbers to base cases
    int first = 0, second = 1;
    int third = 1, fourth = 2;
 
    // declare a current variable
    int curr = 0;
 
    if (n == 0)
        System.out.print(first);
    else if (n == 1 || n == 2)
        System.out.print(second);
 
    else if (n == 3)
        System.out.print(fourth);
 
    else
    {
 
        // Loop to add previous
        // four numbers for
        // each number starting
        // from 4 and then assign
        // first, second, third
        // to second, third, fourth and
        // curr to fourth respectively
        for (int i = 4; i <= n; i++)
        {
            curr = first + second + third + fourth;
            first = second;
            second = third;
            third = fourth;
            fourth = curr;
        }
        System.out.print(curr);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 10;
    printTetra(n);
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


Python3
# A space optimized based Python3 program
# to print the nth tetranacci number
 
# Function to print the N-th
# tetranacci number
def printTetra(n):
 
    if (n < 0):
        return;
 
    # Initialize first four
    # numbers to base cases
    first = 0;
    second = 1;
    third = 1;
    fourth = 2;
 
    # declare a current variable
    curr = 0;
 
    if (n == 0):
        print(first);
    elif (n == 1 or n == 2):
        print(second);
 
    elif (n == 3):
        print(fourth);
 
    else:
 
        # Loop to add previous four numbers
        # for each number starting from 4
        # and then assign first, second,
        # third to second, third, fourth
        # and curr to fourth respectively
        for i in range(4, n + 1):
            curr = first + second + third + fourth;
            first = second;
            second = third;
            third = fourth;
            fourth = curr;
         
    print(curr);
 
# Driver code
n = 10;
printTetra(n);
 
# This code is contributed by mits


C#
// A space optimized based C# program to
// print the nth tetranacci number
using System;
 
class GFG{
     
// Function to print the
// N-th tetranacci number
static void printTetra(int n)
{
    if (n < 0)
        return;
 
    // Initialize first
    // four numbers to base cases
    int first = 0, second = 1;
    int third = 1, fourth = 2;
 
    // declare a current variable
    int curr = 0;
 
    if (n == 0)
        Console.Write(first);
    else if (n == 1 || n == 2)
        Console.Write(second);
 
    else if (n == 3)
        Console.Write(fourth);
 
    else
    {
 
        // Loop to add previous
        // four numbers for
        // each number starting
        // from 4 and then assign
        // first, second, third
        // to second, third, fourth and
        // curr to fourth respectively
        for (int i = 4; i <= n; i++)
        {
            curr = first + second + third + fourth;
            first = second;
            second = third;
            third = fourth;
            fourth = curr;
        }
        Console.Write(curr);
    }
}
 
    // Driver code
    static public void Main ()
    {
         
        int n = 10;
        printTetra(n);
    }
}
 
// This code is contributed ajit


PHP


Javascript


输出:
208

时间复杂度: O(4 N )

更好的解决方案是使用动态编程(记忆化),因为存在多个重叠。
下面给出的是 N=10 的递归树。

rec(10)

                             /         /       \          \

                       rec(9)      rec(8)     rec(7)     rec(6)

              /      /     \     \
                     
          rec(8) rec(7)  rec(6)  rec(5)

在上面的部分递归树中,rec(8)、rec(7)、rec(6)已经解决了两次。在绘制完整的递归树时,已经观察到有许多子问题被一次又一次地解决。所以这个问题具有重叠的子结构属性,可以通过使用 Memoization 或 Tabulation 来避免相同子问题的重新计算。
下面是上述方法的实现。

C++

// A DP based CPP
// program to print
// the nth tetranacci number
#include 
using namespace std;
 
// Function to print the
// N-th tetranacci number
int printTetra(int n)
{
    int dp[n + 5];
    // base cases
    dp[0] = 0;
    dp[1] = dp[2] = 1;
    dp[3] = 2;
 
    for (int i = 4; i <= n; i++)
        dp[i] = dp[i - 1] + dp[i - 2] +
                dp[i - 3] + dp[i - 4];
 
    cout << dp[n];
}
 
// Driver code
int main()
{
    int n = 10;
    printTetra(n);
    return 0;
}

Java

// A DP based Java
// program to print
// the nth tetranacci number
 
class GFG{
// Function to print the
// N-th tetranacci number
static void printTetra(int n)
{
    int[] dp=new int[n + 5];
    // base cases
    dp[0] = 0;
    dp[1] = dp[2] = 1;
    dp[3] = 2;
 
    for (int i = 4; i <= n; i++)
        dp[i] = dp[i - 1] + dp[i - 2] +
                dp[i - 3] + dp[i - 4];
 
    System.out.print(dp[n]);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 10;
    printTetra(n);
}
}
// This code is contributed by mits

蟒蛇3

# A DP based Python3 program to print
# the nth tetranacci number
 
# Function to print the
# N-th tetranacci number
def printTetra(n):
    dp = [0] * (n + 5);
     
    # base cases
    dp[0] = 0;
    dp[1] = 1;
    dp[2] = 1;
    dp[3] = 2;
 
    for i in range(4, n + 1):
        dp[i] = (dp[i - 1] + dp[i - 2] +
                 dp[i - 3] + dp[i - 4]);
 
    print(dp[n]);
 
# Driver code
n = 10;
printTetra(n);
 
# This code is contributed by mits

C#

// A DP based C#
// program to print
// the nth tetranacci number
 
class GFG{
// Function to print the
// N-th tetranacci number
static void printTetra(int n)
{
    int[] dp=new int[n + 5];
    // base cases
    dp[0] = 0;
    dp[1] = dp[2] = 1;
    dp[3] = 2;
 
    for (int i = 4; i <= n; i++)
        dp[i] = dp[i - 1] + dp[i - 2] +
                dp[i - 3] + dp[i - 4];
 
    System.Console.WriteLine(dp[n]);
}
 
// Driver code
static void Main()
{
    int n = 10;
    printTetra(n);
}
}
// This code is contributed by mits

PHP


Javascript


输出:
208

时间复杂度: O(N)
辅助空间: O(N)

上面的时间复杂度是线性的,但它需要额外的空间。通过使用四个变量来跟踪前四个数字,可以在上述解决方案中优化使用的空间。
下面是上述方法的实现。

C++

// A space optimized
// based CPP program to
// print the nth tetranacci number
#include 
using namespace std;
 
// Function to print the
// N-th tetranacci number
void printTetra(int n)
{
    if (n < 0)
        return;
 
    // Initialize first
    // four numbers to base cases
    int first = 0, second = 1;
    int third = 1, fourth = 2;
 
    // declare a current variable
    int curr;
 
    if (n == 0)
        cout << first;
    else if (n == 1 || n == 2)
        cout << second;
 
    else if (n == 3)
        cout << fourth;
 
    else {
 
        // Loop to add previous
        // four numbers for
        // each number starting
        // from 4 and then assign
        // first, second, third
        // to second, third, fourth and
        // curr to fourth respectively
        for (int i = 4; i <= n; i++) {
            curr = first + second + third + fourth;
            first = second;
            second = third;
            third = fourth;
            fourth = curr;
        }
        cout << curr;
    }
}
 
// Driver code
int main()
{
    int n = 10;
    printTetra(n);
    return 0;
}

Java

// A space optimized
// based Java program to
// print the nth tetranacci number
import java.io.*;
import java.util.*;
import java.lang.*;
 
class GFG{
// Function to print the
// N-th tetranacci number
static void printTetra(int n)
{
    if (n < 0)
        return;
 
    // Initialize first
    // four numbers to base cases
    int first = 0, second = 1;
    int third = 1, fourth = 2;
 
    // declare a current variable
    int curr = 0;
 
    if (n == 0)
        System.out.print(first);
    else if (n == 1 || n == 2)
        System.out.print(second);
 
    else if (n == 3)
        System.out.print(fourth);
 
    else
    {
 
        // Loop to add previous
        // four numbers for
        // each number starting
        // from 4 and then assign
        // first, second, third
        // to second, third, fourth and
        // curr to fourth respectively
        for (int i = 4; i <= n; i++)
        {
            curr = first + second + third + fourth;
            first = second;
            second = third;
            third = fourth;
            fourth = curr;
        }
        System.out.print(curr);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 10;
    printTetra(n);
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)

蟒蛇3

# A space optimized based Python3 program
# to print the nth tetranacci number
 
# Function to print the N-th
# tetranacci number
def printTetra(n):
 
    if (n < 0):
        return;
 
    # Initialize first four
    # numbers to base cases
    first = 0;
    second = 1;
    third = 1;
    fourth = 2;
 
    # declare a current variable
    curr = 0;
 
    if (n == 0):
        print(first);
    elif (n == 1 or n == 2):
        print(second);
 
    elif (n == 3):
        print(fourth);
 
    else:
 
        # Loop to add previous four numbers
        # for each number starting from 4
        # and then assign first, second,
        # third to second, third, fourth
        # and curr to fourth respectively
        for i in range(4, n + 1):
            curr = first + second + third + fourth;
            first = second;
            second = third;
            third = fourth;
            fourth = curr;
         
    print(curr);
 
# Driver code
n = 10;
printTetra(n);
 
# This code is contributed by mits

C#

// A space optimized based C# program to
// print the nth tetranacci number
using System;
 
class GFG{
     
// Function to print the
// N-th tetranacci number
static void printTetra(int n)
{
    if (n < 0)
        return;
 
    // Initialize first
    // four numbers to base cases
    int first = 0, second = 1;
    int third = 1, fourth = 2;
 
    // declare a current variable
    int curr = 0;
 
    if (n == 0)
        Console.Write(first);
    else if (n == 1 || n == 2)
        Console.Write(second);
 
    else if (n == 3)
        Console.Write(fourth);
 
    else
    {
 
        // Loop to add previous
        // four numbers for
        // each number starting
        // from 4 and then assign
        // first, second, third
        // to second, third, fourth and
        // curr to fourth respectively
        for (int i = 4; i <= n; i++)
        {
            curr = first + second + third + fourth;
            first = second;
            second = third;
            third = fourth;
            fourth = curr;
        }
        Console.Write(curr);
    }
}
 
    // Driver code
    static public void Main ()
    {
         
        int n = 10;
        printTetra(n);
    }
}
 
// This code is contributed ajit

PHP


Javascript


输出:
208

时间复杂度: O(N)
辅助空间: O(1)

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