### fabonacii series

ASSALAM O ALAIKUM
kindly clear me FABONACII SERIES ....
fib(previous):

if previous > 13
return previous*1.61803398875; //feel free to dig up more decimal places for it
else
return hardcoded/lookup value of the answer.

or you can do it with an ugly brute force function. This is terrible, just like computing factorials is terrible.

int x = 1;
int y = 1;
int tmp;
int i;
for(i = 0; i < 10; i++)
{
cout << x+y << endl;
tmp = y;
y = x;
x = x+tmp;
}
Last edited on
 ``1234567891011`` ``````#include #include using namespace std; int main() { int N, f; cout << "Which term would you like? "; cin >> N; f = pow( 1.618033989, N ) / 2.236067978 + 0.5; cout << f; }``````

 ```Which term would you like? 10 55```

It works, but I don't suggest that you hand this in @champ195.

Fundamentally, it's a sequence of terms
1,1,2,3,5,8 ...
with a given rule to derive each term from the previous two. Since you need to do something REPEATEDLY you need to consider LOOPS. Have a read of that section in
http://www.cplusplus.com/doc/tutorial/control/

Alternatively, you could use a recursive function, once you've written one for your factorial example.
Last edited on
or that, if you don't have previous or if you want to avoid the low values issue.
 ``12345678`` ``````#include int fib( int N ) { return ( N * N - N ? ( N < 0 ? fib( N + 2 ) - fib( N + 1 ) : fib( N - 2 ) + fib( N - 1 ) ) : N ); } int main() { for ( int N = -10; N <= 10; N++ ) std::cout << fib( N ) << " "; }``````

 `-55 34 -21 13 -8 5 -3 2 -1 1 0 1 1 2 3 5 8 13 21 34 55 `
There is a disturbing lack of template metaprogramming here.

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include template struct fibonacci { enum { value = fibonacci::value + fibonacci::value }; }; template <> struct fibonacci<1> { enum { value = 1 }; }; template <> struct fibonacci<0> { enum { value = 0 }; }; int main() { std::cout << fibonacci<0>::value << std::endl; std::cout << fibonacci<1>::value << std::endl; std::cout << fibonacci<2>::value << std::endl; std::cout << fibonacci<3>::value << std::endl; std::cout << fibonacci<4>::value << std::endl; std::cout << fibonacci<5>::value << std::endl; std::cout << fibonacci<6>::value << std::endl; std::cout << fibonacci<7>::value << std::endl; // I gave up on trying to get // print_fibonacci_numbers<0, 2, 5, 7, 11, 15>(); // or // print_fibonacci_numbers(0, 2, 5, 7, 11, 15); // to work... my mind is numb and I'm so confused }``````
Last edited on
 ``1234567891011121314`` ``````#include #include using namespace std; using INT = unsigned long long; INT fib( int N ) { return (INT)( pow( 1.618033988749894848, N ) / 2.2360679775 + 0.5 ); } void printfib( int n ) { cout << "Term " << n << " in the Fibonacci sequence is " << fib( n ) << '\n'; } template void printfib( int n, T... arg ) { printfib( n ); printfib( arg... ); } int main() { printfib( 0, 2, 5, 7, 11, 15 ); printfib( 51, 9, 11 ); }``````