diff --git a/homework/fibonacci/fibonacci.hpp b/homework/fibonacci/fibonacci.hpp index 3faab622..4d42dc60 100644 --- a/homework/fibonacci/fibonacci.hpp +++ b/homework/fibonacci/fibonacci.hpp @@ -1,11 +1,28 @@ #pragma once +#include int fibonacci_iterative(int sequence) { - // TODO: Your implementation goes here - return 0; + if(sequence <= 1) { + return sequence; + } + + int a = 0; + int b = 1; + + for(int i = 2; i <= sequence; ++i) { + int temp = b; + b += a; + a = temp; + } + + return b; } int fibonacci_recursive(int sequence) { - // TODO: Your implementation goes here - return 0; + if(sequence <= 1) { + return sequence; + } + + int result = fibonacci_recursive(sequence - 1) + fibonacci_recursive(sequence - 2); + return result; }