Skip to content

Commit 103e634

Browse files
committed
Add fibonacci iterative implementation
1 parent 65ed827 commit 103e634

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

homework/fibonacci/fibonacci.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#pragma once
22

33
int fibonacci_iterative(int sequence) {
4-
// TODO: Your implementation goes here
5-
return 0;
4+
int current_number{0};
5+
int next_number{1};
6+
for (int i{0}; i < sequence; ++i) {
7+
int new_number = current_number + next_number;
8+
current_number = next_number;
9+
next_number = new_number;
10+
}
11+
return current_number;
612
}
713

814
int fibonacci_recursive(int sequence) {

0 commit comments

Comments
 (0)