0

I need to write a program in C++ to find the closest Fibonacci number to the input number. I patched together a program that tells the Fibonacci number in the nth place, I've included it's code. Next is this task of finding the closest number to the input. What I've gathered is that I probably need to use a binary search/decision tree to rule out the bad numbers. I can generate the Fibonacci numbers so I'm that close. I need a point in the right direction to get me going. I'm pretty sure Binet's formula is involved as well as the golden ratio. Here's the code for my original program:

int fib(int n)

{
    if (n <= 1)
        return (n);
    return fib(n - 1) + fib(n - 2);
}

int main()
{
    cout << "Enter a number greater than -1 and see what the n'th place in the fibbonaci sequence equals.\n";
    cin >> n;
    fib(n);

    if (n >= 0)
        cout << "n'th Fibonacci number is " << fib(n) << "\n"; 
    else
        cout << "\nInvalid number.\n\n";
    return 0;
}
  • 1
    What's the upper limit for `n`? Your approach will probably break for any n that is not too small. – L. F. Mar 19 '20 at 05:17
  • What's wrong with just iterating over fibonacci numbers until you hit or exceed the search value? – JohnFilleau Mar 19 '20 at 05:24
  • @John Then check also the previous one to see which one is the nearest. – Costantino Grana Mar 19 '20 at 06:57
  • @L.F. I wasn't given an upper limit. – Ryan Dunnion Mar 19 '20 at 21:17
  • This should be in your C++ book: how many bytes are in an int? Based on that, what's the largest number it can store? – JohnFilleau Mar 19 '20 at 21:28
  • By "iterating" I just mean use a loop. Certainly you've covered loops by now? – JohnFilleau Mar 19 '20 at 21:29
  • @John Yes we covered loops. There are 4 bytes in an integer. The largest number that can be stored in an int is 2147483647. Is that correct? – Ryan Dunnion Mar 19 '20 at 22:58
  • That sounds about right. You can use that to determine what your maximum return value of `fib` is. Anything outside of this will have unexpected results. – JohnFilleau Mar 19 '20 at 22:59
  • @RyanDunnion That’s technically wrong (the upper limit of int is an implementation defined integer greater than or equal to 32767), but the point is that simple approaches only operate when the number is not too big (Fibonacci numbers grow quickly). You’ll need a bignum library if you want to handle arbitrarily big numbers (of course you are still limited by available memory). – L. F. Mar 20 '20 at 01:15

1 Answers1

0

So I found some code that calculated the index for me. I made minor adjustments for input.

#include <iostream> 
using namespace std;

int findIndex(int n) 
{
    if (n <= 1) 
        return n; 

    int a = 0, b = 1, c = 1; 
    int res = 1; 
    while (c < n) 
    { 
        c = a + b; 
        res++; 
        a = b; 
        b = c; 
    } 
    return res; 
} 

int main() 
{ 
    int fib_number;
    cout << "Please enter a single integer number to see the closest index in the Fibonacci sequence.\n";
    cin >> fib_number;
    int result = findIndex(fib_number); 
    cout << "The Fibonacci index is " << result << "."; 
}