Milliman Interview Question

Write an algorithm that gets the Nth number in the Fibionce Sequence

Interview Answer

Anonymous

Sep 4, 2016

Above answer has a typo: int Fib ( int N ) { return N == 0 || N == 1 ? 1 : Fib(N-1) + Fib(N); } This is also slow.. There is a faster implementation private static long Fibonacci(int n) { long a = 0L; long b = 1L; for (int i = 31; i >= 0; i--) { long d = a * (b * 2 - a); long e = a * a + b * b; a = d; b = e; if ((((uint)n >> i) & 1) != 0) { long c = a + b; a = b; b = c; } } return a; }