Fibonacci Word - GeeksforGeeks

30 Apr, 2021
Like Fibonacci numbers, a Fibonacci word. is a specific sequence of binary digits (or symbols from any two-letter alphabet). The Fibonacci word is formed by repeated concatenation in the same way that the Fibonacci numbers are formed by repeated addition. But unlike the fibonacci number, Fibonacci word has its first two terms different from each other.  
In Fibonacci word,
  S(0) = 0, 
  S(1) = 01, 
  S(2) = 010,
  S(3) = 01001
   ..... 
where S(n) = S(n-1) + S(n-2) and + 
represents the concatenation of 
strings. 
The task is to find nth Fibonacci word for a given number n.Examples:  
Input : n = 4
Output : S(4) = 01001010

Input : n = 2
Output : S(2) = 010
 
Just like in program of Fibonacci number, we use the iterative concept of finding nth Fibonacci number here for finding nth Fibonacci word we can use the iterative concept. So for finding n-th Fibonacci word we will take two string Sn and Sn_1 which represent S(n) and S(n-1) respectively and on each iteration we will update tmp = Sn, Sn = Sn + Sn_1 and Sn_1 = tmp in this way we can find nth fibonacci word. 
C++
Java
Python3
C#
Javascript
// program for nth Fibonacci word#include<bits/stdc++.h>using namespace std; // Returns n-th Fibonacci wordstring fibWord(int n){    string Sn_1 = "0";    string Sn = "01";    string tmp;    for (int i=2; i<=n; i++)    {        tmp = Sn;        Sn += Sn_1;        Sn_1 = tmp;    }     return Sn;} // driver programint main(){    int n = 6;    cout << fibWord(n);    return 0;}
// Java program for nth Fibonacci wordimport java.util.*; class Eulerian{    // Returns n-th Fibonacci word    public static String fibWord(int n)    {        String Sn_1 = "0";        String Sn = "01";        String tmp;        for (int i=2; i<=n; i++)        {            tmp = Sn;            Sn += Sn_1;            Sn_1 = tmp;        }         return Sn;    }         // driver code    public static void main(String[] args)    {        int n = 6;        System.out.print(fibWord(n));    }} // This code is contributed by rishabh_jain
# Python3 program for nth Fibonacci word # Returns n-th Fibonacci worddef fibWord(n):    Sn_1 = "0"    Sn = "01"    tmp = ""    for i in range(2, n + 1):        tmp = Sn        Sn += Sn_1        Sn_1 = tmp    return Sn # driver programn = 6print (fibWord(n)) # This code is contributed by Sachin Bisht
// C# program for nth Fibonacci wordusing System; class GFG{    // Returns n-th Fibonacci word    public static String fibWord(int n)    {        String Sn_1 = "0";        String Sn = "01";        String tmp;        for (int i = 2; i <= n; i++)        {            tmp = Sn;            Sn += Sn_1;            Sn_1 = tmp;        }         return Sn;    }         // Driver code    public static void Main()    {        int n = 6;        Console.WriteLine(fibWord(n));    }} // This code is contributed by vt_m
<script> // program for nth Fibonacci word // Returns n-th Fibonacci wordfunction fibWord(n){    var Sn_1 = "0";    var Sn = "01";    var tmp;    for (var i = 2; i <= n; i++)    {        tmp = Sn;        Sn += Sn_1;        Sn_1 = tmp;    }     return Sn;} // driver programvar n = 6;document.write( fibWord(n)); // This code is contributed by noob2000.</script>
Output:  
010010100100101001010
 
YouTubeGeeksforGeeks501K subscribersFibonacci Word | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:41•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=G2CttokqYwo" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
noob2000
Fibonacci
Strings
Strings
Fibonacci
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
Naive algorithm for Pattern Searching
Vigenère Cipher
Hill Cipher
How to Append a Character to a String in C
Convert character array to string in C++
Minimum changes to a string to make all substrings distinct
Converting Roman Numerals to Decimal lying between 1 to 3999
sprintf() in C
Print all subsequences of a string