diff --git a/fibonacci/cxx/06_general/test_fibonacci.cpp b/fibonacci/cxx/06_general/test_fibonacci.cpp index 697ac45..6fa1a06 100644 --- a/fibonacci/cxx/06_general/test_fibonacci.cpp +++ b/fibonacci/cxx/06_general/test_fibonacci.cpp @@ -4,8 +4,7 @@ #include "fibonacci.hpp" TEST_CASE("fibonacci", "test") { - const std::vector> cases{ - {0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}}; + const std::vector> cases{{0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}}; for (auto&& nv : cases) { const auto n = std::get<0>(nv); const auto v = std::get<1>(nv); diff --git a/fibonacci/cxx/07_negative/test_fibonacci.cpp b/fibonacci/cxx/07_negative/test_fibonacci.cpp index a28c05e..939739a 100644 --- a/fibonacci/cxx/07_negative/test_fibonacci.cpp +++ b/fibonacci/cxx/07_negative/test_fibonacci.cpp @@ -4,8 +4,7 @@ #include "fibonacci.hpp" TEST_CASE("fibonacci", "test") { - const std::vector> cases{ - {-2, -1}, {-1, 1}, {0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}}; + const std::vector> cases{{-2, -1}, {-1, 1}, {0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}}; for (auto &&nv : cases) { const auto n = std::get<0>(nv); const auto v = std::get<1>(nv); diff --git a/fibonacci/cxx/08_linear/fibonacci.hpp b/fibonacci/cxx/08_linear/fibonacci.hpp index f6e2895..5206837 100644 --- a/fibonacci/cxx/08_linear/fibonacci.hpp +++ b/fibonacci/cxx/08_linear/fibonacci.hpp @@ -6,15 +6,21 @@ inline int fibonacci(int n) { int v0 = 0; int v1 = 1; if (n < 0) { - for (int i = -1; i >= n; --i) - std::tie(v0, v1) = std::make_tuple(v1 - v0, v0); + for (int i = -1; i >= n; --i) { + auto v0_old = v0; + v0 = v1 - v0_old; + v1 = v0_old; + } return v0; } if (n == 0) return v0; if (n == 1) return v1; - for (int i = 2; i <= n; ++i) - std::tie(v1, v0) = std::make_tuple(v0 + v1, v1); + for (int i = 2; i <= n; ++i) { + auto v1_old = v1; + v1 = v0 + v1_old; + v0 = v1_old; + } return v1; } diff --git a/fibonacci/cxx/08_linear/test_fibonacci.cpp b/fibonacci/cxx/08_linear/test_fibonacci.cpp index fe989be..fdbfc8d 100644 --- a/fibonacci/cxx/08_linear/test_fibonacci.cpp +++ b/fibonacci/cxx/08_linear/test_fibonacci.cpp @@ -4,9 +4,7 @@ #include "fibonacci.hpp" TEST_CASE("fibonacci", "test") { - const std::vector> cases{ - {-2, -1}, {-1, 1}, {0, 0}, {1, 1}, - {2, 1}, {3, 2}, {10, 55}, {40, 102334155}}; + const std::vector> cases{{-2, -1}, {-1, 1}, {0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}, {40, 102334155}}; for (auto &&nv : cases) { const auto n = std::get<0>(nv); const auto v = std::get<1>(nv); diff --git a/fibonacci/cxx/09_multiprec/fibonacci.hpp b/fibonacci/cxx/09_multiprec/fibonacci.hpp index 43c55dd..1c21d40 100644 --- a/fibonacci/cxx/09_multiprec/fibonacci.hpp +++ b/fibonacci/cxx/09_multiprec/fibonacci.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include namespace mp = boost::multiprecision; @@ -9,15 +8,21 @@ inline mp::cpp_int fibonacci(int n) { mp::cpp_int v0 = 0; mp::cpp_int v1 = 1; if (n < 0) { - for (int i = -1; i >= n; --i) - std::tie(v0, v1) = std::make_tuple(v1 - v0, v0); + for (int i = -1; i >= n; --i) { + auto v0_old = v0; + v0 = v1 - v0_old; + v1 = v0_old; + } return v0; } if (n == 0) return v0; if (n == 1) return v1; - for (int i = 2; i <= n; ++i) - std::tie(v1, v0) = std::make_tuple(v0 + v1, v1); + for (int i = 2; i <= n; ++i) { + auto v1_old = v1; + v1 = v0 + v1_old; + v0 = v1_old; + } return v1; } diff --git a/fibonacci/cxx/09_multiprec/test_fibonacci.cpp b/fibonacci/cxx/09_multiprec/test_fibonacci.cpp index b27b142..541c168 100644 --- a/fibonacci/cxx/09_multiprec/test_fibonacci.cpp +++ b/fibonacci/cxx/09_multiprec/test_fibonacci.cpp @@ -4,18 +4,7 @@ #include "fibonacci.hpp" TEST_CASE("fibonacci", "test") { - const std::vector> cases{ - {-2, -1}, - {-1, 1}, - {0, 0}, - {1, 1}, - {2, 1}, - {3, 2}, - {10, 55}, - {40, 102334155}, - {100, mp::cpp_int("354224848179261915075")}, - {250, - mp::cpp_int("7896325826131730509282738943634332893686268675876375")}}; + const std::vector> cases{{-2, -1}, {-1, 1}, {0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}, {40, 102334155}, {100, mp::cpp_int("354224848179261915075")}}; for (auto &&nv : cases) { const auto n = std::get<0>(nv); const auto v = std::get<1>(nv); diff --git a/fibonacci/cxx/10_lucas/recursion.hpp b/fibonacci/cxx/10_lucas/recursion.hpp index 23dea0a..af91ed9 100644 --- a/fibonacci/cxx/10_lucas/recursion.hpp +++ b/fibonacci/cxx/10_lucas/recursion.hpp @@ -1,21 +1,26 @@ #pragma once -#include #include namespace mp = boost::multiprecision; inline mp::cpp_int recursion(mp::cpp_int v0, mp::cpp_int v1, int n) { if (n < 0) { - for (int i = -1; i >= n; --i) - std::tie(v0, v1) = std::make_tuple(v1 - v0, v0); + for (int i = -1; i >= n; --i) { + auto v0_old = v0; + v0 = v1 - v0_old; + v1 = v0_old; + } return v0; } if (n == 0) return v0; if (n == 1) return v1; - for (int i = 2; i <= n; ++i) - std::tie(v1, v0) = std::make_tuple(v0 + v1, v1); + for (int i = 2; i <= n; ++i) { + auto v1_old = v1; + v1 = v0 + v1_old; + v0 = v1_old; + } return v1; } diff --git a/fibonacci/cxx/10_lucas/test_fibonacci.cpp b/fibonacci/cxx/10_lucas/test_fibonacci.cpp index b27b142..541c168 100644 --- a/fibonacci/cxx/10_lucas/test_fibonacci.cpp +++ b/fibonacci/cxx/10_lucas/test_fibonacci.cpp @@ -4,18 +4,7 @@ #include "fibonacci.hpp" TEST_CASE("fibonacci", "test") { - const std::vector> cases{ - {-2, -1}, - {-1, 1}, - {0, 0}, - {1, 1}, - {2, 1}, - {3, 2}, - {10, 55}, - {40, 102334155}, - {100, mp::cpp_int("354224848179261915075")}, - {250, - mp::cpp_int("7896325826131730509282738943634332893686268675876375")}}; + const std::vector> cases{{-2, -1}, {-1, 1}, {0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}, {40, 102334155}, {100, mp::cpp_int("354224848179261915075")}}; for (auto &&nv : cases) { const auto n = std::get<0>(nv); const auto v = std::get<1>(nv); diff --git a/fibonacci/cxx/10_lucas/test_lucas.cpp b/fibonacci/cxx/10_lucas/test_lucas.cpp index b27c0b2..1f4f71e 100644 --- a/fibonacci/cxx/10_lucas/test_lucas.cpp +++ b/fibonacci/cxx/10_lucas/test_lucas.cpp @@ -4,19 +4,10 @@ #include "lucas.hpp" TEST_CASE("lucas", "test") { - const std::vector> cases{ - {-2, 3}, - {-1, -1}, - {0, 2}, - {1, 1}, - {2, 3}, - {3, 4}, - {10, 123}, - {40, 228826127}, - {100, mp::cpp_int("792070839848372253127")}, - {250, - mp::cpp_int("17656721319717734662791328845675730903632844218828123")}}; - for (auto &&[n, v] : cases) { + const std::vector> cases{{-2, 3}, {-1, -1}, {0, 2}, {1, 1}, {2, 3}, {3, 4}, {10, 123}, {40, 228826127}, {100, mp::cpp_int("792070839848372253127")}}; + for (auto &&nv : cases) { + const auto n = std::get<0>(nv); + const auto v = std::get<1>(nv); REQUIRE(lucas(n) == v); if (n > 0) REQUIRE(lucas(n) > 0); diff --git a/tdd-tutorial.key b/tdd-tutorial.key index 8dfcc75..b3b466f 100644 Binary files a/tdd-tutorial.key and b/tdd-tutorial.key differ