-6

I try to proceed the following code but the outcome is Segmentation fault (core dumped). How could I fix the error? Also, will my code produce the fibonacci-orial of 100? My code is as follows.

#include <iostream>

using namespace std;
int fib(int);
int main()
{
    int n;
    int result[100];
    for (int i=0;i<100;i++){
        result[i] = fib(i);
    } for (int i=0;i<100;i++){
        cout << i+ 1 << " : " << result[i] << endl;
    }

}
int fib(int n){
    int re;
    if (n==0||n==1){
        re = 1;
    } else{
        re = fib(n)*fib(n - 1);
    }
    return re;
}

The Fibonacci-orial

Fibonacci sequence F(n), on the positive integers, are defined as such:

1. F(1) = 1
2. F(2) = 1
3. F(n) = F(n-1) + F(n-2), where n is an integer and n > 2

The Fibonacci-orial of a positive integer is the product of [F(1), F(2), ..., F(n)].

The fibonacci-orial of 20

n   Fibonacci-orial of n
1   1
2   1
3   2
4   6
5   30
6   240
7   3120
8   65520
9   2227680
10  122522400
11  10904493600
12  1570247078400
13  365867569267200
14  137932073613734400
15  84138564904377984000
16  83044763560621070208000
17  132622487406311849122176000
18  342696507457909818131702784000
19  1432814097681520949608649339904000
20  9692987370815489224102512784450560000
m.s.
  • 16,063
  • 7
  • 53
  • 88
  • 2
    The right tool to solve such problems is to use your debugger, but not to ask at Stack Overflow before you did so. Tell us all your observations you made when inspecting your code stepping through line by line in 1st place. Also you might want to read [**How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)**] At least leave us with a **[MCVE]** that reproduces your problem. (This is a personal stock comment provided by πάντα ῥεῖ™) – πάντα ῥεῖ Aug 15 '16 at 10:33
  • segmentation fault in a case like this probably means the recursive `fib` call went too far.... double check your termination condition and if that's all right, you might need to rewrite it in an iterative fashion rather than recursion because the depth is too much – Adam D. Ruppe Aug 15 '16 at 10:35
  • And this fib(n) is calculating fib(n+1) or -1, depends how you look on it. Either way, it doesn't help the clarity to introduce the +-1 on this level. You probably wanted it to store into the `result[]` from `0` index? Rather waste 0th element of `result[101]`, or use `result[n-1] = fib(n)`, keeping `n` inside of `fib()` going from 1 upward. (fixing the terminating `if` of course). So your `fib()` will then look directly as the theory formula => makes finding bugs easier. Again all the `for` loops should be `for (n = 1; n <= 100; ++n)`. Stay human, bend to machine logic only as last resort. – Ped7g Aug 15 '16 at 10:52

3 Answers3

0

Just change the line:

re = fib(n)*fib(n - 1);

for

re = n * fib(n - 1);

or you will end up in an infinite loop.

Regarding the calculation of the first 100 factorial numbers, yes, your code can calculate them but the results will not fit in an int variable. For more info there are multiple answers in SO about calculating the factorial of large numbers: 1, 2, 3.

Community
  • 1
  • 1
FrankS101
  • 2,112
  • 6
  • 26
  • 40
0

You should change the line re = fib(n) * fib(n - 1);

into: re = fib(n-2) + fib(n - 1);

I think, fibonacci as calculated with an Addition - by calling fib(n-2) instead of fib(n), you remove your endless recursive function-calls - and I think, it matches the correct algorithm for fibonacci.

Schnied
  • 36
  • 1
0
int main()
{
    int n;
    int result[100];
    int iPrevResult = 1;
    for (int i=0;i<100;i++)
    {
        iPrevResult  = iPrevResult* fib(i);
        result[i] = iPrevResult;
    } 

    for (int i=1;i<100;i++)
    {
        cout << i+ 1 << " : " << result[i] << endl;
    }
}
int fib(int n){
    if (n<=1){
       return 1;
    } 
    return (fib(n-1) +fib(n-2));
}