Pergunta de entrevista da empresa Amazon

Coding the fibonacci algorithm.

Respostas da entrevista

Sigiloso

23 de mar. de 2011

A fibonacci sequence is a a sequence of numbers in which each number equals the sum of the two preceding numbers. If N is the number then: (0) + (1) + ... (N-2) + (N-1) In C, non-recursively, this looks like: int fib(int n) { int first = 0, second = 1; int tmp; while (n--) { tmp = first+second; first = second; second = tmp; } return first; }

Sigiloso

6 de set. de 2011

BruteForce is O(n) Can be accelarated by matrix multiplication

Sigiloso

6 de mai. de 2012

#include int Fibonacci(int); main() { int n, i = 0, c; scanf("%d",&n); printf("Fibonacci series\n"); for ( c = 1 ; c <= n ; c++ ) { printf("%d\n", Fibonacci(i)); i++; } return 0; } int Fibonacci(int n) { if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else return ( Fibonacci(n-1) + Fibonacci(n-2) ); }