Skip to content

Commit

Permalink
Task. Fibonacci. An implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
5arnA committed Feb 6, 2024
1 parent 65ed827 commit 2587dbf
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions homework/fibonacci/fibonacci.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
#pragma once

int fibonacci_iterative(int sequence) {
// TODO: Your implementation goes here
return 0;
if (sequence <= 0) {
return 0;
}

if (sequence <= 2) {
return 1;
}

int fib{};
for (int count = 2, fib_1 = 1, fib_2 = 1, t = 0;
count <= sequence;
t = fib_2, fib_2 += fib_1, fib_1 = t, ++count) {
fib = fib_2;
}
return fib;
}

int fibonacci_recursive(int sequence) {
// TODO: Your implementation goes here
return 0;
if (sequence <= 0) {
return 0;
}

if (sequence <= 2) {
return 1;
}
return fibonacci_recursive(sequence - 2) + fibonacci_recursive(sequence - 1);
}

0 comments on commit 2587dbf

Please sign in to comment.