Determining the individual letters of Fibonacci strings?

fibonacci sequence
fibonacci string in c
fibonacci words
write a program that generates and prints the fibonacci words of order 0 through 5

The Fibonacci strings are defined as follows:

  • The first Fibonacci string is "a"
  • The second Fibonacci string is "bc"
  • The (n + 2)nd Fibonacci string is the concatenation of the two previous Fibonacci strings.

For example, the first few Fibonacci strings are

a
bc
abc
bcabc
abcbcabc

The goal is, given a row and an offset, to determine what character is at that offset. More formally:

Input: Two integers separated by a space - K and P(0 < K ≤ 109), ( < P ≤ 109), where K is the line number of the Fibonacci string and P is the position number in a row.

Output: The desired character for the relevant test: "a", "b" or "c". If P is greater than the kth row (K ≤ 109), it is necessary to derive «No solution»

Example:

input: 18 58

output: a

I wrote this code to solve the problem:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    int k, p;
    string s1 = "a";
    string s2 = "bc";
    vector < int >fib_numb;
    fib_numb.push_back(1);
    fib_numb.push_back(2);
    cin >> k >> p;
    k -= 1;
    p -= 1;
    while (fib_numb.back() < p) {
        fib_numb.push_back(fib_numb[fib_numb.size() - 1] + fib_numb[fib_numb.size() - 2]);
    }
    if (fib_numb[k] <= p) {
        cout << "No solution";
        return 0;
    }
    if ((k - fib_numb.size()) % 2 == 1)
        k = fib_numb.size() + 1;
    else
        k = fib_numb.size();
    while (k > 1) {
        if (fib_numb[k - 2] > p)
            k -= 2;
        else {
            p -= fib_numb[k - 2];
            k -= 1;
        }
    }
    if (k == 1)
        cout << s2[p];
    else
        cout << s1[0];
    return 0;
}

Is it correct? How would you have done?

Fibonacci Word, Like Fibonacci numbers, a Fibonacci word. is a specific sequence of binary digits (or symbols from any two-letter alphabet). 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 My Personal Notes arrow_drop_up. In the infinite Fibonacci word, the ratio (number of letters)/(number of zeroes) is φ, as is the ratio of zeroes to ones. The infinite Fibonacci word is a balanced sequence: Take two factors of the same length anywhere in the Fibonacci word.

I guess your general idea should be OK, but I don't see how your code is going to deal with larger values of K, because the numbers will get enormous quickly, and even with large integer libraries it might take virtually forever to compute fibonacci(10^9) exactly.

Fortunately, you are only asked about the first 10^9 characters. The string will reach that many characters already on the 44th line (f(44) = 1134903170).

And if I'm not mistaken, from there on the first 10^9 characters will be simply alternating between the prefixes of line 44 and 45, and therefore in pseudocode:

def solution(K, P):
   if K > 45:
       if K % 2 == 0:
           return solution(44, P)
       else:
           return solution(45, P)
   #solution for smaller values of K here 

Fibonacci word, Thus, the first five Fibonacci strings are: "a", "b", "ba", "bab", "babba". It is guaranteed that strings s i aren't empty and consist only of characters "a" and "b". The individual conductor strands of their cables are arranged in a Phi proportion to the others allowing them to be uniquely musical and pure. According to Cardas: “An infinitely indivisible progression known as the Fibonacci sequence or Golden Section is the key to resonance control.

I found this. I did not do a pre-check (get the size of the k-th fibo string to test p againt it) because if the check is successful you'll have to compute it anyway. Of course as soon as k becomes big, you may have an overflow issue (the length of the fibo string is an exponential function of the index n...).

#include <iostream>
#include <string>

using namespace std;

string fibo(unsigned int n)
{
    if (n == 0)
        return "a";
    else if (n == 1)
        return "bc";
    else
        return fibo(n - 2) + fibo(n - 1);

}

int main()
{
    unsigned int k, p;
    cin >> k >> p;
    --k;
    --p;
    string fiboK = fibo(k);
    if (p > fiboK.size())
        cout << "No solution" << endl;
    else
        cout << fiboK[p] << endl;
    return 0;
}

EDIT: ok, I now see your point, i.e. checking in which part of the k-th string p resides (i.e. in string k - 2 or k - 1, and updating p if needed). Of course this is the good way to do it, since as I was saying above my naive solution will explode quite too quickly.

Your way looks correct to me from an algorithm point of view (saves memory and complexity).

Problem - 177G1, Fibonacci numbers and the golden section in nature, art, geometry, architecture, music and even for calculating pi! Puzzles and investigations. of Fibonacci and Phi. The golden section numbers are also written using the Greek letters Phi Phi​  Given a sequence of words, print all anagrams together | Set 2 Check if sum of Fibonacci elements in an Array is a Fibonacci number or not Count numbers divisible by K in a range with Fibonacci digit sum for Q queries

I would have computed the K-th Fibonacci String, and then retrieve the P-th character of it. Something like that:

#include <iostream>
#include <string>
#include <vector>

std::string FibonacciString(unsigned int k)
{
    std::vector<char> buffer;
    buffer.push_back('a');
    buffer.push_back('b');
    buffer.push_back('c');

    unsigned int curr = 1;
    unsigned int next = 2;

    while (k --)
    {
        buffer.insert(
            buffer.end(),
            buffer.begin(),
            buffer.end());

        buffer.erase(
            buffer.begin(),
            buffer.begin() + curr);

        unsigned int prev = curr;
        curr = next;
        next = prev + next;
    }

    return std::string(
        buffer.begin(),
        buffer.begin() + curr);
}

int main(int argc, char** argv)
{
    unsigned int k, p;
    std::cin >> k >> p;

    -- p;
    -- k;

    std::string fiboK = FibonacciString(k);
    if (p > fiboK.size())
        std::cout << "No solution";
    else
        std::cout << fiboK[p];
    std::cout << std::endl;
    return 0;
}

It does use more memory than your version since it needs to store both the N-th and the (N+1)-th Fibonacci string at every instant. However, since it is really close to the definition, it does work for every value.

Your algorithm seems to have some issue when k is large while p is small. The test fib_num[k] < p will dereference an item outside of the range of the array with k = 30 and p = 1, won't it ?

Fibonacci Numbers, the Golden section and the Golden String, For a string $S$ let the unique set of characters that occur in it one or more output in a single line "**Dynamic**" if the given string is dynamic,  In this video, we want to solve the following problem: Given a string, develop an algorithm to return the first occurring uppercase letter. We require both an iterative and recursive solution to

I made another example where each corresponding number of Fibonnaci series corresponds to the letter in the alfabet. So for 1 is a, for 2 is b, for 3 is c, for 5 is e... etc:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string a = "abcdefghijklmnopqrstuvwxyz"; //the alphabet
    string a1 = a.substr(0,0); string a2 = a.substr(1,1); string nexT = a.substr(0,0);

    nexT = a1 + a2;

   while(nexT.length() <= a.length())
    {
        //cout << nexT.length() << ", ";             //show me the Fibonacci numbers
        cout << a.substr(nexT.length()-1,1) << ", "; //show me the Fibonacci letters
        a1 = a2;
        a2 = nexT;
        nexT = a1 + a2;
    }
    return 0;
}

Output: a, b, c, e, h, m, u,

Fibonacci String, letters of each word AB and replacing them by an A (which we write as A' for any point set generated by substitution, as we will prove in a more general setting It follows that all Fibonacci strings belong to a single local isomorphism class:  Separate the individual characters from a string : ----- Input the string : w3resource.com The characters of the string are : w 3 r e s o u r c e .

Quasicrystals and Geometry, Recurson for strings, similar to, but different from the fibonacci words in DNA are valuable in determining a particular person's parentage and other aspects of​  A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

AMS :: Feature Column :: Words and More Words, A factor u of a string y is a cover of y if every letter of y falls | Find, read We consider the problem of finding quasiperiodicities in a Fibonacci string. period​ic repetition the o ccurrences of the single periods do not overlap. Try and let us know, if it works. For experiments you make your own xls file so that you put the string you want. Once the code works and give desired resutls then it would be easy for you to debug your problem in your current xls file, as you mentioned due to spaces or anything.

(PDF) Quasiperiodicities in Fibonacci strings, theoretical importance, Fibonacci strings are often cited as worst-case examples for algorithms Furthermore, if a given string x defined on an arbitrary alphabet A can be written in the form x = yz?z for runs of squares using only a single triple of integers. each run is determined by one of the squares F,' (Theorem 2.4). Here, we are going to learn how to find the Nth Fibonacci number using Dynamic programming in C++. Submitted by Ritik Aggarwal, on November 07, 2018 Problem: Compute the N th Fibonacci number. You are given a number N. You have to find the N th Fibonacci number. 0 th Fibonacci number is 0 and first Fibonacci number is 1. Sample inputs:

Comments
  • Have you tried it? And is this homework?
  • What happens when you compile and run the code ? Does it build OK ? Does it run OK ? Does it give the expected result ?
  • It is not homework. I do it for fun. I think this code is correct
  • Post it here codereview.stackexchange.com
  • +1. And for smaller values you can use something like the following solution(n, P): if (P > F(n-2)) solution(n-1, P-F(n-2)) else solution(n-2, P) (F(n) is the fibonacci number). No need of maintaining large strings.
  • 4 6 must be "No solution" is not it? 3 2 must be b... etc.
  • I agree: 4th string is "bcabc" and the 3rd one is "abc".
  • Please read this tratt.net/laurie/tech_articles/articles/tail_call_optimization
  • I think a bit of a problem with actually creating the string is that 10 ^ 9 th string might contain quite an astronomic amount of characters? It might not even be possible to calculate the exact value of 10^9th fibonacci number because the value will have more than 10^8 decimal digits. - I suspect there must be a solution that doesn't require doing either.
  • You're right. However, when I made my answer, the question was incorrectly formatted and the constraints on the value of both k and p was not readable. I'll think of a better solution. This code can however be used to check that other solution give the correct answer (for relatively small value of k).