Home c++ Production of Fibonacci numbers C++

Production of Fibonacci numbers C++

Author

Date

Category

We need recursion to calculate the product of Fibonacci numbers from 1 to n
In theory, all the rules, but in practice only for the case N = 4. Please tell me what the problem is?

# include & lt; iostream & gt;
Using Namespace STD;
Int FIB (int n) {
  IF (N & LT; = 1) Return 1;
  ELSE RETURN FIB (N - 1) + FIB (N - 2);
}
INT FIBPRODUCT (int m) {
  if (m & lt; = 1) Return 1;
  ELSE RETURN FIBPRODUCT (FIB (M-1)) * (FIB (M));
}
INT MAIN (Void) {
  COUT & LT; & LT; FibProduct (4);
  Return 0;
}

Answer 1, Authority 100%

I have a feeling that you need to consider the product of all Fibonacci numbers from the first to N-th. Then it is necessary to simply correctly write a function of the work – the product of all numbers from the first to N-th is equal to the product of all from the first to N-1st on the N-e number of Fibonacci:

# include & lt; iostream & gt;
Using Namespace STD;
Int FIB (int n) {
  IF (N & LT; = 1) Return 1;
  ELSE RETURN FIB (N - 1) + FIB (N - 2);
}
INT FIBPRODUCT (INT N) {
  IF (N & LT; = 1) Return 1;
  RETURN FIBPRODUCT (N-1) * FIB (N);
}
INT MAIN (Void) {
  COUT & LT; & LT; FibProduct (4) & lt; & lt; Endl;
  Return 0;
}

Answer 2

int fibproduct (int m) {
  if (m & lt; = 1) Return 1;
  ELSE RETURN FIBPRODUCT (FIB (M-1)) * (FIB (M));
}

This is a monster function,

See what she does:

fibproduct (fib (M-1))

with M & GT; 4 you have fib (M-1) & gt; 4 and therefore the function will never finish its implementation, but on the contrary, it will only increase and increase the number of iterations and at a certain point you will clarify the stack and the program will fall with the error

You had to do this:

else return fibproduct (m - 1) * fib (m);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions