📜  Tetranacci数字

📅  最后修改于: 2021-04-29 14:35:37             🧑  作者: Mango

四面体数是由递归关系定义的斐波那契数的一般化

对于n> = 4。它们代表斐波那契n步数的n = 4情况。 n = 0,1,的前几项是0,1,1,2,4,8,15,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


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


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


输出:
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)被求解了两次。在绘制完整的递归树时,已经观察到有很多子问题可以一次又一次地解决。因此,此问题具有“重叠子结构”属性,可以通过使用“记忆化”或“制表”来避免相同子问题的重新计算。

下面是上述方法的实现

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


输出:
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)

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


输出:
208

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