Tribonacci Word

Like Fibonacci word, a Tribonacci word. is a specific sequence of digits. The Tribonacci word is formed by repeated concatenation in the same way that the Fibonacci word is formed by repeated addition. But unlike the fibonacci word, Tribonacci word is formed by repeated addition of last three terms and it has its first three terms different from each other.

In Tribonacci word,
  S(0) = 1, 
  S(1) = 12, 
  S(2) = 1213,
  S(3) = 1213121 
  ..... 
where S(n) = S(n-1) + S(n-2) + S(n-3) and + 
represents the concatenation of 
strings. 

The task is to find nth Tribonacci word for a given number n.

Examples:

Input : n = 4
Output : S(4) = 1213121121312

Input : n = 3
Output : S(3) = 1213121



Just like in program of Fibonacci word, we use the iterative concept of finding nth Fibonacci word here for finding nth Tribonacci word we can use the iterative concept. So for finding nth Tribonacci word we will take three string Sn_1, Sn_2 and Sn_3 which represent S(n-1), S(n-2) and S(n-3) respectively and on each iteration we will update tmp = Sn_3, Sn_3 = Sn_3 + Sn_2 + Sn_1, Sn_1 = Sn_2 and Sn_2 = tmp in this way we can find nth tribonacci word.

C++

// C++ program for nth Fibonacci word
#include <bits/stdc++.h>
using namespace std;

// Returns n-th Tribonacci word
string tribWord(int n) {
  string Sn_1 = "1";
  string Sn_2 = "12";
  string Sn_3 = "1213";
  string tmp;
  for (int i = 3; i <= n; i++) {
    tmp = Sn_3;
    Sn_3 += (Sn_2 + Sn_1);
    Sn_1 = Sn_2;
    Sn_2 = tmp;
  }

  return Sn_3;
}

// driver program
int main() {
  int n = 6;
  cout << tribWord(n);
  return 0;
}

Java

// program for nth Tribonacci word
class GFG {

// Returns n-th Tribonacci word
static String tribWord(int n) {
    String Sn_1 = "1";
    String Sn_2 = "12";
    String Sn_3 = "1213";
    String tmp;
    for (int i = 3; i <= n; i++) {
    tmp = Sn_3;
    Sn_3 += (Sn_2 + Sn_1);
    Sn_1 = Sn_2;
    Sn_2 = tmp;
    }

    return Sn_3;
}

// Driver code
public static void main(String[] args) {
    int n = 6;
    System.out.print(tribWord(n));
}
}
// This code is contributed by Anant Agarwal.

Python3

# Python3 Program for nth
# Tribonacci word

# Returns n-th Tribonacci word
def tribWord(n):
    Sn_1 = "1"
    Sn_2 = "12"
    Sn_3 = "1213"
    for i in range(3, n+1):
        tmp = Sn_3
        Sn_3 += (Sn_2 + Sn_1)
        Sn_1 = Sn_2
        Sn_2 = tmp

    return Sn_3

# Driver code
n = 6
print(tribWord(n))

# This code is contributed By Anant Agarwal.

C#

// C# program for nth Tribonacci word
using System;

class GFG {

    // Returns n-th Tribonacci word
    static string tribWord(int n)
    {
        string Sn_1 = "1";
        string Sn_2 = "12";
        string Sn_3 = "1213";
        string tmp;
        
        for (int i = 3; i <= n; i++) {
            tmp = Sn_3;
            Sn_3 += (Sn_2 + Sn_1);
            Sn_1 = Sn_2;
            Sn_2 = tmp;
        }
    
        return Sn_3;
    }
    
    // Driver code
    public static void Main() {
        
        int n = 6;
        
        Console.WriteLine(tribWord(n));
    }
}

// This code is contributed by vt_m.

PHP

<?php

// Returns n-th Tribonacci word
function tribWord($n) 
{
$Sn_1 = "1";
$Sn_2 = "12";
$Sn_3 = "1213";
$tmp;
for ($i = 3; $i <= $n; $i++) 
{
    $tmp = $Sn_3;
    $Sn_3 .= ($Sn_2.$Sn_1);
    $Sn_1 = $Sn_2;
    $Sn_2 = $tmp;
}

return $Sn_3;
}

// Driver Code
$n = 6;
echo tribWord($n);

// This code is contributed by mits 

?>


Output:

12131211213121213121121312131211213121213121


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Improved By : Mithun Kumar




Practice Tags :
Article Tags :

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.






User Actions