diff --git a/c/src/p0015.c b/c/src/p0015.c index c5d334f1..898bfebb 100644 --- a/c/src/p0015.c +++ b/c/src/p0015.c @@ -22,10 +22,8 @@ How many such routes are there through a 20×20 grid? #include "include/macros.h" #include "include/math.h" -#define lattice_paths(height, width) (n_choose_r(height + width, height)) - uint64_t EMSCRIPTEN_KEEPALIVE p0015() { - return lattice_paths(20, 20); + return n_choose_r(40, 20); } PROGRAM_TAIL("%" PRIu64, p0015) diff --git a/cplusplus/src/p0013.cpp b/cplusplus/src/p0013.cpp index aa27e153..b462c0f0 100644 --- a/cplusplus/src/p0013.cpp +++ b/cplusplus/src/p0013.cpp @@ -217,23 +217,19 @@ static const uint64_t ten18 = 1000000000000000000; static const uint64_t ten10 = 10000000000; uint64_t EMSCRIPTEN_KEEPALIVE p0013() { - uint64_t high = 0, med = 0, low = 0; + uint64_t arr[3] = { 0 }; for (uint8_t i = 0; i < 100; ++i) { - low += numbers[i][2]; - med += numbers[i][1]; - high += numbers[i][0]; - if (low > ten18) { - med += low / ten18; - low %= ten18; - } - if (med > ten18) { - high += med / ten18; - med %= ten18; - } + for (uint8_t j = 0; j < 3; ++j) + arr[j] += numbers[i][j]; + for (uint8_t j = 2; j > 0; --j) + if (arr[j] > ten18) { + arr[j - 1] += arr[j] / ten18; + arr[j] %= ten18; + } } - while (high > ten10) - high /= 10; - return high; + while (arr[0] > ten10) + arr[0] /= 10; + return arr[0]; } PROGRAM_TAIL(p0013) diff --git a/cplusplus/src/p0015.cpp b/cplusplus/src/p0015.cpp index 2640221e..21788c6c 100644 --- a/cplusplus/src/p0015.cpp +++ b/cplusplus/src/p0015.cpp @@ -21,10 +21,8 @@ How many such routes are there through a 20×20 grid? #include "include/macros.hpp" #include "include/math.hpp" -#define lattice_paths(height, width) (n_choose_r(height + width, height)) - uint64_t EMSCRIPTEN_KEEPALIVE p0015() { - return lattice_paths(20, 20); + return n_choose_r(40, 20); } PROGRAM_TAIL(p0015) diff --git a/cplusplus/src/p0017.cpp b/cplusplus/src/p0017.cpp index 87bb6a2d..92b3f4d1 100644 --- a/cplusplus/src/p0017.cpp +++ b/cplusplus/src/p0017.cpp @@ -95,18 +95,14 @@ std::string ToString(uint64_t n) { uint64_t EMSCRIPTEN_KEEPALIVE p0017() { uint64_t answer = 0; + std::string filter[2] = {" ", "-"}; + size_t pos, i; for (uint32_t x = 1; x < 1001; x += 1) { std::string str = ToString(x); - size_t pos = str.find(" "); - while (pos != std::string::npos) { - str.replace(pos, 1, ""); - pos = str.find(" ", pos + 1); - } - pos = str.find("-"); - while (pos != std::string::npos) { - str.replace(pos, 1, ""); - pos = str.find("-", pos + 1); - } + for (pos = 0, i = 0; i < 2; i++) + while ((pos = str.find(" ", pos)) != std::string::npos) + str.replace(pos, 1, ""); + answer += str.length(); } return answer;