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
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cmath>
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.
1
2
3
4
5
6
7
8
#include <iostream>

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

template <size_t n>
struct fibonacci {
  enum { value = fibonacci<n - 1>::value + fibonacci<n - 2>::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
Variadic templates, @Ganado?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cmath>
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 <typename... T> void printfib( int n, T... arg ) { printfib( n ); printfib( arg... ); }

int main()
{
   printfib( 0, 2, 5, 7, 11, 15 );
   printfib( 51, 9, 11 );
}
Last edited on
thanks
@lastchance from my understanding that's still computing fib(int) at run time (but it's O(1) so it doesn't really matter, in this case). I was trying to get for-loops to work at compile time to calculate fibonacci<i> to <n> but it sadly isn't that simple, apparently. But anyway... that's a discussion for another time, don't wanna hijack champ's thread.
if you want to optimize it, its hard to beat a lookup table. The series gets big enough fast enough that this is not that many values. I think it explodes out of a 64 bit int in < 100 values, but that is from memory.
Topic archived. No new replies allowed.