diff --git a/homework/fibonacci/fibonacci.hpp b/homework/fibonacci/fibonacci.hpp index 3faab622..07f2d2bb 100644 --- a/homework/fibonacci/fibonacci.hpp +++ b/homework/fibonacci/fibonacci.hpp @@ -1,11 +1,37 @@ #pragma once int fibonacci_iterative(int sequence) { - // TODO: Your implementation goes here - return 0; + if (sequence < 0 || sequence >= 46) { + return -1; + } + if (sequence == 0) { + return 0; + } + if (sequence == 1) { + return 1; + } + + int n_2 = 0; + int n_1 = 1; + int n = 0; + for (auto i = 1; i < sequence; ++i) { + n = n_1 + n_2; + n_2 = n_1; + n_1 = n; + } + return n; } int fibonacci_recursive(int sequence) { - // TODO: Your implementation goes here - return 0; + if (sequence < 0 || sequence >= 46) { + return -1; + } + if (sequence == 0) { + return 0; + } + if (sequence == 1) { + return 1; + } + + return fibonacci_recursive(sequence - 1) + fibonacci_recursive(sequence - 2); }