From 51b5b4c1abd917c6768b9be4b17922ef8c42df2e Mon Sep 17 00:00:00 2001 From: Bartosz Rybarczyk Date: Wed, 12 Aug 2026 12:03:04 +0200 Subject: [PATCH 1/3] Implement iterative Fibonacci function --- homework/fibonacci/fibonacci.hpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/homework/fibonacci/fibonacci.hpp b/homework/fibonacci/fibonacci.hpp index 3faab622..e69f0ee6 100644 --- a/homework/fibonacci/fibonacci.hpp +++ b/homework/fibonacci/fibonacci.hpp @@ -1,8 +1,25 @@ #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) { From 10eb604a4a9162999431702717508b52dc55cdbb Mon Sep 17 00:00:00 2001 From: Bartosz Rybarczyk Date: Wed, 12 Aug 2026 12:32:34 +0200 Subject: [PATCH 2/3] Implement recursive Fibonacci function --- homework/fibonacci/fibonacci.hpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/homework/fibonacci/fibonacci.hpp b/homework/fibonacci/fibonacci.hpp index e69f0ee6..15696d40 100644 --- a/homework/fibonacci/fibonacci.hpp +++ b/homework/fibonacci/fibonacci.hpp @@ -23,6 +23,16 @@ int fibonacci_iterative(int sequence) { } 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); + } From fdbf2e76d2b3e29e6ce41c3fc5b95bf68772f0e6 Mon Sep 17 00:00:00 2001 From: Bartekr100 Date: Wed, 12 Aug 2026 14:03:14 +0200 Subject: [PATCH 3/3] Fix formatting --- homework/fibonacci/fibonacci.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/homework/fibonacci/fibonacci.hpp b/homework/fibonacci/fibonacci.hpp index 15696d40..07f2d2bb 100644 --- a/homework/fibonacci/fibonacci.hpp +++ b/homework/fibonacci/fibonacci.hpp @@ -23,7 +23,7 @@ int fibonacci_iterative(int sequence) { } int fibonacci_recursive(int sequence) { - if (sequence < 0 || sequence >= 46) { + if (sequence < 0 || sequence >= 46) { return -1; } if (sequence == 0) { @@ -33,6 +33,5 @@ int fibonacci_recursive(int sequence) { return 1; } - return fibonacci_recursive(sequence-1) + fibonacci_recursive(sequence -2); - + return fibonacci_recursive(sequence - 1) + fibonacci_recursive(sequence - 2); }