Find the number of a given number in the Fibonacci sequence

The task itself: http://www.codeabbey.com/index/task_view/fibonacci-sequence--ru, my code:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<long double> fib{0, 1};
    for(int i = 1; i < 1000; i++) // По условию все числа из первой тысячи
                                  // значений последовательности
    {
        fib.push_back(fib[i] + fib[i-1]);
    }

    int N; // Первая строчка должна содержать количество проводимых тестов
    cin >> N;

 long double value[N];
for(int i = 0; i < N; i++)
{
    cin >> value[i];
}

    int begin, end;
    for (int i = 0; i < N; i++)
    {
        if(value[i] > fib[fib.size() / 2]) // Если value находится во второй половине последовательности
        {
            begin = fib.size() / 2;
            while(value[i] > fib[begin]) 
            {
                begin /= 2;
            }
            begin *= 2; // Возвращаюсь на шаг назад
                        // ибо после выполнения цикла
                        // value выходит из диапозона
            end = fib.size();
            for( ; begin < end; begin++)
            {
                if(fib[begin] == value[i]) {cout << begin << " "; break;}
            }
        }

        else
        {
            end = fib.size() / 2;
            while (value[i] < fib[end])
            {
                end /= 2;
            }
            end *= 2; // То же самое что и в первой ветви
            begin = 0;
            for ( ; begin < end; begin++)
            {
                if(fib[begin] == value[i]) {cout << begin << " "; break;}
            }
        }
    }

    return 0;
}

It would be possible not to make such a game and just write

for(int i = 0; i < N; i++)
    for(int j = 0; j < fib.size(); j++)
        if(value[i] == fib[j]) cout << j + 1 << " ";

But while reading one smart book, I became interested in recreating a simplified version of the binary_search() algorithm, and in general, here :)

The site with the task also kindly offers data entry to check the program, I was given such:

For the sake of a small test, I tried to enter only a few of them, and more specifically - the very first, second and fourth. To my surprise, all I got on the way out was nothing.

Disappointed in my code, I redid the code to

vector<long double> fib{0, 1}; 
    long double value;
         cin >> value;

    int i = 1;
    int number_of_element = 1;
    while (value > fib.back()) {
        fib.push_back(fib[i] + fib[i-1]);
        i++;
        number_of_element++;
    }
    cout << fixed << "Element: " << fib[i-1] << " It's number: " << --number_of_element;

To find out what I'm wrong about and at the same time check if the numbers just come across more than the first thousand. I entered the same thing in turn: the first, second, and third numbers from Test data. In all three results the number obtained by the program was almost identical to the one entered, up to about 19 characters (I lost count many times on this it is not accurate), the element number also did not exceed a thousand. Based on this, I concluded that the machine simply had an error after a certain number of characters. Is there any way to fix this problem?

Author: Harry, 2017-01-19

2 answers

In general, a link to a similar task For each test, print in a separate line the minimum angle between the arrows in degrees in the format given in the example (I even took half of the code from there).

The code is written on the knee, it can (and should) be made more accurate, but I think you can understand the idea.

And the idea is very simple - we just do the operation modulo a prime number (you can have more than one, you can not have a prime number). And we believe that if it coincided modulo, then it will also match without the module.

Why this is correct, yes, you can just check that there are no identical elements in the array of fibonacci numbers within the first thousand.

char z[40000];

long long mod(long long mm){
    long long r = 0;
    for (int i=0; z[i]; i++){
        r %= mm;
        r*=10;
        r+= z[i] - '0';
    }
    return r%=mm;
}

int main ()
{
    vector< long long > xx;
    xx.push_back(0);
    xx.push_back(1);
    for (int i=0;i<=1000;i++)
        xx.push_back(
              (xx[xx.size() -1] +  xx[xx.size() -2]) % 1000000000039LL
        );
    int N;
    cin >> N;
    for (int i=0;i<N;i++){
        cin >> z;
        long long R = mod(1000000000039LL);
        for (int I = 0; I <= 1000; I++)
            if (R == xx[I]){
                cout << I<<" ";
                break;
            }
    }
}

You can make it even easier, but then there will be a problem if the input is not a fibonacci number. This is to use Binet's formula and stupidly prolog the main part. The accuracy is low, but it is hard to make a mistake 2 times. Well, check the first 10 numbers with your hands. The advantage of this method is that you can not even read the entire line, it is important know only the first 2 digits (or maybe even 1) and the number of digits.


In general, this site is suitable only for beginners, even the most difficult tasks are solved without any problems.

 3
Author: pavel, 2017-04-13 12:53:25

I suggest a simpler method :) Since there are two whole units, we have an ambiguity problem. So I work for numbers starting from the second one, assuming one is the second Fibonacci number...

Here is the code that gives the number of the number, but does not check if the number is entered correctly... So it will give an answer and NOT for the Fibonacci number. Let's hope that the input is not served as such - we were not required to recognize whether this number is correct, right?

int main(int argc, const char * argv[])
{
    string s;
    while(getline(cin,s,'\n'))
    {
        int ex = s.length()-1;
        s.insert(1,".");
        double x = stod(s);
        int n = (log10(5.0)/2.0+log10(x)+ex)/log10((1.0+sqrt(5.0))/2)+0.5;
        cout << n << endl;
    }
}

Checked for numbers from the second and up to 1001-th (209-digit):), the answers are correct...

Justification:

enter a description of the image here

From where

enter a description of the image here

If we count

enter a description of the image here

To

enter a description of the image here

Then we calculate by the formula :)

 2
Author: Harry, 2017-01-19 15:18:07