summaryrefslogtreecommitdiffstats
path: root/01-algorithmic_toolbox/01-intro/01-fibonacci
diff options
context:
space:
mode:
Diffstat (limited to '01-algorithmic_toolbox/01-intro/01-fibonacci')
-rw-r--r--01-algorithmic_toolbox/01-intro/01-fibonacci/fibonacci.cpp19
1 files changed, 18 insertions, 1 deletions
diff --git a/01-algorithmic_toolbox/01-intro/01-fibonacci/fibonacci.cpp b/01-algorithmic_toolbox/01-intro/01-fibonacci/fibonacci.cpp
index adc7404..c2564b8 100644
--- a/01-algorithmic_toolbox/01-intro/01-fibonacci/fibonacci.cpp
+++ b/01-algorithmic_toolbox/01-intro/01-fibonacci/fibonacci.cpp
@@ -7,10 +7,27 @@ int calc_fib(int n) {
return calc_fib(n - 1) + calc_fib(n - 2);
}
+int fib(int n)
+{
+ int a, b, c, i;
+
+ if (n <= 1)
+ return n;
+
+ a = 0; b = 1;
+ for(i = 1; i < n; i++) {
+ c = a + b;
+ a = b;
+ b = c;
+ }
+
+ return b;
+}
+
int main() {
int n = 0;
std::cin >> n;
- std::cout << calc_fib(n) << '\n';
+ std::cout << fib(n) << '\n';
return 0;
}