I'm trying to add the extension to the Fibonacci numbers to take into account negative indexes. The extension is Fib(-n) = (-n)^(n+1) * Fib(n) I have attempted to implement this in c++ but I'm running into a problem and I'm not sure how to get around it.
#include <iostream>
#include <math.h>
int fib(int n) {
if ( n < 0 ){
return ( pow(-1,-n+1 ) * fib(-n) );
}else if (n == 0){
return 1;
}else{
return fib(n-1) + fib(n-2);
}
}
int main(void){
std::cout << "fib("<< -2<<") = " << fib(-2) << std::endl;
return 0;
}
This gives me a segfault, any idea why this is?
EDIT: I figured out what the problem is. Forgot a base case and this caused an infinite recursion, which in turn caused a segfault.