Como la palabra de Fibonacci , una palabra de Tribonacci. es una secuencia específica de dígitos. La palabra de Tribonacci se forma por concatenación repetida de la misma manera que la palabra de Fibonacci se forma por adición repetida. Pero a diferencia de la palabra Fibonacci, la palabra Tribonacci está formada por la adición repetida de los últimos tres términos y tiene sus tres primeros términos diferentes entre sí.
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.
La tarea es encontrar la enésima palabra de Tribonacci para un número dado n.
Ejemplos:
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 ?>
Javascript
<script> // javascript program for nth Fibonacci word // Returns n-th Tribonacci word function tribWord(n) { var Sn_1 = "1"; var Sn_2 = "12"; var Sn_3 = "1213"; var tmp; for (var 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 var n = 6; document.write( tribWord(n)); // This code is contributed by noob2000. </script>
Publicación traducida automáticamente
Artículo escrito por Shivam.Pradhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA