-3

I started to learn C++ and my homework is to write a code where you can enter 5 numbers and the program will tell you for each number whether it is a Fibonacci number or not.

I also tried using a do/while-loop in the isFibonacci function instead of the for-loop, but that did not fix the problem.

#include <iostream>
#include <cstdio>

using namespace std;

//function to test whether a number is a Fibonacci number or not
bool isFibonacci (int i) 
{
    //special cases with 0 and 1:
    if ( i == 0 || i ==1) {
        return true;  
    } 
    //for all other numbers:
    int Fib1;
    int Fib2;
    int Fib3;
    for (Fib3=0; Fib3>=i; Fib3++) {
        Fib3 = Fib1 + Fib2;
        Fib1 = Fib2;
        Fib2 = Fib3;
        if (Fib3==i){
            return true;
        } 
        else {
            return false;
        } 
    }
} 

int main () 
{
    bool result;
    int numbers[5]; 
    int i;
    //asking for the 5 numbers
    cout << "Please enter 5 numbers;" << endl;
    cin >> numbers[0] >> numbers[1] >> numbers[2] >> numbers[3] >> numbers[4];

    // giving back the result
    for (i=0; i<5; i++) {
        result=isFibonacci (numbers[i]);
        if (result == true) {
            cout << "Your number " << numbers[i] << " is a Fibonacci  number!" << endl;
        } 
        else {
            cout << "Your number " << numbers[i] << " is not a Fibonacci number!" << endl;
        } 
    } 
    return 0; 
}  

The first Fibonacci numbers are (0),1,1,2,3,5,8,12. So when I enter 5 numbers, for example 1,2,3,4,5 I should get a "yes" for 1,2,3 and 5, but a "no" for 4. However, my program claims that except for 1, none of these numbers are Fibonacci numbers.

A M
  • 14,694
  • 5
  • 19
  • 44
mdv
  • 3
  • 1

1 Answers1

0

Basically your approach was a good idea. But you made some implementation errors in your check function. Like not initialized variables and wrong calculations. And look at you for loop.

Additionally. There will be a problem with big numbers.

Many very smart people, explored the Fibonacci numbers. There are whole books available. Also a Wikipedia article. See here.

Or look into that book:

The(Fabulous) FIBONACCI Numbers by Alfred Posamentierand Ingmar Lehmann

Or also here on stackoverflow

Therefore I will not reinvent the wheel. Here is your revised software:

#include <iostream>
#include <cmath>
#include <numeric>


// Positive integer ? is a Fibonacci number
// If and only if one of 5?2 + 4 and 5?2 - 4 is a perfect square
// from The(Fabulous) FIBONACCI Numbers by Alfred Posamentierand Ingmar Lehmann
// Function to test whether a number is a Fibonacci number or not
bool isFibonacci(int w)
{
    {
        double x1 = 5 * std::pow(w, 2) + 4;
        double x2 = 5 * std::pow(w, 2) - 4;

        long x1_sqrt = static_cast<long>(std::sqrt(x1));
        long x2_sqrt = static_cast<long>(std::sqrt(x2));

        return (x1_sqrt * x1_sqrt == x1) || (x2_sqrt * x2_sqrt == x2);
    }
}

int main()
{
    bool result;
    int numbers[5];
    int i;
    //asking for the 5 numbers
    std::cout << "Please enter 5 numbers;" << std::endl;
    std::cin >> numbers[0] >> numbers[1] >> numbers[2] >> numbers[3] >> numbers[4];

    // giving back the result
    for (i = 0; i < 5; i++) {
        result = isFibonacci(numbers[i]);
        if (result == true) {
            std::cout << "Your number " << numbers[i] << " is a Fibonacci  number!" << std::endl;
        }
        else {
            std::cout << "Your number " << numbers[i] << " is not a Fibonacci number!" << std::endl;
        }
    }
    return 0;
}
A M
  • 14,694
  • 5
  • 19
  • 44
  • 1
    In case anyone is interested in the math, the reason this "works" is because of implicit properties of the [fibonacci sequence closed form](http://mathonline.wikidot.com/a-closed-form-of-the-fibonacci-sequence). If you haven't hit deriving these formulas from linear recurrence sequences in your math studies yet, prepare to enjoy it because its one of the more fun periods of collegiate math. – WhozCraig Jul 14 '19 at 18:44
  • That helps a lot, thank you very much! I just started and still have a lot to learn – mdv Jul 20 '19 at 10:16