I came across an interesting concept Fibonacci word. It is formed by repeated concatenation of symbols, usually two alphabets, using the Fibonacci numbers way.
For sample lets start with the symbol “a” and “ab”.
Here are the first few words
F0 = a
F1 = ab
F2 = aba
F3 = abaab
F4 = abaababa
F5 = abaababaabaab
Let’s do this coding but first make a integer version of the program.
Now let’s make the string version of the program.
The integer version and the string version of the programs are very similar to each other. Let’s try to make a generic function. This is our first attempt.
This program has two problem. First the initialization of the first and second elements of Fibonacci word. It can be easily solved by passing the initial values as a parameter. Here is a modified version of the program.
But there is still another problem. There is still a compilation error.
Error 1 Operator ‘+’ cannot be applied to operands of type ‘T’ and ‘T’
We can solve this problem with the dynamic keyword of C#. We can convert the last and second last item of the Fibonacci word using dynamic keyword before applying the concatenation words.
Here is a modified version of Fibonacci word.