diff --git a/README.rst b/README.rst index ca5619ce..e59aff39 100644 --- a/README.rst +++ b/README.rst @@ -58,12 +58,12 @@ Olivia's Project Euler Solutions +------------+----------------------------+--------+-------------------+ | Language | Version | Solved | Status | +============+============================+========+===================+ -| C | C99+ in: |clang|, |br| | 24 | |Ci| |br| | +| C | C99+ in: |clang|, |br| | 25 | |Ci| |br| | | | |gcc|, |pcc|, & |tcc| |br| | | |C-Cov| |br| | | | C11+ in: |msvc| [1]_ | | |CodeQL| |br| | | | | | |C-lint| | +------------+----------------------------+--------+-------------------+ -| C++ | C++98+ in: |br| |clang|, & | 17 | |Cpi| |br| | +| C++ | C++98+ in: |br| |clang|, & | 18 | |Cpi| |br| | | | |gcc| |br| | | |Cp-Cov| |br| | | | C++14+ in: |msvc| [1]_ | | |CodeQL| |br| | | | | | |Cp-lint| | diff --git a/c/README.rst b/c/README.rst index 4ffa385b..4d7d54cf 100644 --- a/c/README.rst +++ b/c/README.rst @@ -179,6 +179,8 @@ Problems Solved - ☒ `14 <./src/p0014.c>`__ - ☒ `15 <./src/p0015.c>`__ - ☒ `16 <./src/p0016.c>`__ +- ☒ `17 <./src/p0017.c>`__ +- ☒ `19 <./src/p0019.c>`__ - ☒ `20 <./src/p0020.c>`__ - ☒ `22 <./src/p0022.c>`__ - ☒ `34 <./src/p0034.c>`__ diff --git a/c/src/p0019.c b/c/src/p0019.c new file mode 100644 index 00000000..f4139d21 --- /dev/null +++ b/c/src/p0019.c @@ -0,0 +1,61 @@ +/* +Project Euler Problem 19 + +This one ended up being very easy thanks to the time library + +Problem: + +You are given the following information, but you may prefer to do some research +for yourself. + + 1 Jan 1900 was a Monday. + Thirty days has September, + April, June and November. + All the rest have thirty-one, + Saving February alone, + Which has twenty-eight, rain or shine. + And on leap years, twenty-nine. + A leap year occurs on any year evenly divisible by 4, but not on a century + unless it is divisible by 400. + +How many Sundays fell on the first of the month during the twentieth century +(1 Jan 1901 to 31 Dec 2000)? +*/ +#ifndef EULER_P0019 +#define EULER_P0019 +#include +#include +#include +#include + +uint16_t p0019() { + uint16_t answer = 0; + struct tm date = {0}; + + for (int year = 1901; year <= 2000; ++year) { + for (int month = 0; month < 12; ++month) { + date.tm_year = year - 1900; + date.tm_mon = month; + date.tm_mday = 1; + + if (mktime(&date) == -1) { + fprintf(stderr, "mktime failed to normalize the date.\n"); + return -1; + } + + if (date.tm_wday == 0) { + ++answer; + } + } + } + return answer; +} + + +#ifndef UNITY_END +int main(int argc, char const *argv[]) { + printf("%" PRIu16 "\n", p0019()); + return 0; +} +#endif +#endif diff --git a/c/test.c b/c/test.c index a994bc95..5dcce683 100644 --- a/c/test.c +++ b/c/test.c @@ -17,6 +17,7 @@ #include "src/p0015.c" #include "src/p0016.c" #include "src/p0017.c" +#include "src/p0019.c" #include "src/p0020.c" #include "src/p0022.c" #include "src/p0025.c" @@ -48,6 +49,7 @@ const Answer answers[] = { {15, 137846528820, p0015}, {16, 1366, p0016}, {17, 21124, p0017}, + {19, 171, (uint64_t (*)()) p0019}, {20, 648, p0020}, {22, 871198282, p0022}, {25, 4782, p0025}, diff --git a/c/test_euler.py b/c/test_euler.py index aff51c37..483a99bf 100644 --- a/c/test_euler.py +++ b/c/test_euler.py @@ -23,7 +23,7 @@ answers = { x: get_answer(x) for x in ( *range(1, 18), - 20, + *range(19, 21), 22, 25, 30, diff --git a/cplusplus/src/p0019.cpp b/cplusplus/src/p0019.cpp new file mode 100644 index 00000000..667ae892 --- /dev/null +++ b/cplusplus/src/p0019.cpp @@ -0,0 +1,59 @@ +/* +Project Euler Problem 19 + +This one ended up being very easy thanks to the ctime library + +Problem: + +You are given the following information, but you may prefer to do some research +for yourself. + + 1 Jan 1900 was a Monday. + Thirty days has September, + April, June and November. + All the rest have thirty-one, + Saving February alone, + Which has twenty-eight, rain or shine. + And on leap years, twenty-nine. + A leap year occurs on any year evenly divisible by 4, but not on a century + unless it is divisible by 400. + +How many Sundays fell on the first of the month during the twentieth century +(1 Jan 1901 to 31 Dec 2000)? +*/ +#ifndef EULER_P0019 +#define EULER_P0019 +#include +#include +#include +#include + +uint16_t p0019() { + uint16_t answer = 0; + struct tm date = {0}; + + for (int year = 1901; year <= 2000; ++year) { + for (int month = 0; month < 12; ++month) { + date.tm_year = year - 1900; + date.tm_mon = month; + date.tm_mday = 1; + + if (mktime(&date) == -1) { + throw std::runtime_error("mkdtime failed to normalize the date."); + } + if (date.tm_wday == 0) { + ++answer; + } + } + } + return answer; +} + + +#ifndef UNITY_END +int main(int argc, char const *argv[]) { + std::cout << p0019() << std::endl; + return 0; +} +#endif +#endif diff --git a/cplusplus/test.cpp b/cplusplus/test.cpp index 459618ff..ec18a5e2 100644 --- a/cplusplus/test.cpp +++ b/cplusplus/test.cpp @@ -17,6 +17,7 @@ #include "src/p0015.cpp" #include "src/p0016.cpp" #include "src/p0017.cpp" +#include "src/p0019.cpp" #include "src/p0020.cpp" #include "src/p0022.cpp" #include "src/p0034.cpp" @@ -47,6 +48,7 @@ static const Answer answers[] = { {15, 137846528820, p0015}, {16, 1366, p0016}, {17, 21124, p0017}, + {19, 171, (uint64_t (*)()) p0019}, {20, 648, p0020}, {22, 871198282, p0022}, {34, 40730, p0034}, diff --git a/cplusplus/test_euler.py b/cplusplus/test_euler.py index 2e6b33cb..f4525f25 100644 --- a/cplusplus/test_euler.py +++ b/cplusplus/test_euler.py @@ -30,7 +30,7 @@ 9, 11, *range(13, 18), - 20, + *range(19, 21), 22, 34, 76, diff --git a/docs/index.rst b/docs/index.rst index 35bc3efd..831d6445 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -112,7 +112,7 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`18` | | | | | |:py-d:`0018`|:rs-d:`0018`| +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`19` | | | | |:js-d:`0019`|:py-d:`0019`|:rs-d:`0019`| +|:prob:`19` |:c-d:`0019` |:cp-d:`0019`| | |:js-d:`0019`|:py-d:`0019`|:rs-d:`0019`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`20` |:c-d:`0020` |:cp-d:`0020`|:cs-d:`0020`|:ja-d:`0020`|:js-d:`0020`|:py-d:`0020`|:rs-d:`0020`| +-----------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/src/c/p0019.rst b/docs/src/c/p0019.rst new file mode 100644 index 00000000..8ba1a6c0 --- /dev/null +++ b/docs/src/c/p0019.rst @@ -0,0 +1,22 @@ +C Implementation of Problem 19 +============================== + +View source code :source:`c/src/p0019.c` + +Solution +-------- + +.. c:function:: uint16_t p0019() + +.. c:function:: int main(int argc, char const *argv[]) + + .. note:: + + This function is only present in the Python test runner, or when compiling as a standalone program. + It is not present when compiling for the Unity test runner. + +.. literalinclude:: ../../../c/src/p0019.c + :language: C + :linenos: + +.. tags:: calendar, combinatorics diff --git a/docs/src/cplusplus/p0019.rst b/docs/src/cplusplus/p0019.rst new file mode 100644 index 00000000..95de569c --- /dev/null +++ b/docs/src/cplusplus/p0019.rst @@ -0,0 +1,21 @@ +C++ Implementation of Problem 19 +================================ + +View source code :source:`cplusplus/src/p0019.cpp` + +Solution +-------- + +.. cpp:function:: uint16_t p0019() + +.. cpp:function:: int main(int argc, char const *argv[]) + + .. note:: + + This function is only present in the Python test runner, or when compiling as a standalone program. + +.. literalinclude:: ../../../cplusplus/src/p0019.cpp + :language: C++ + :linenos: + +.. tags:: calendar, combinatorics diff --git a/javascript/README.rst b/javascript/README.rst index 47be0a44..66e58138 100644 --- a/javascript/README.rst +++ b/javascript/README.rst @@ -114,6 +114,7 @@ Problems Solved - ☒ `15 <./src/p0015.js>`__ - ☒ `16 <./src/p0016.js>`__ - ☒ `17 <./src/p0017.js>`__ +- ☒ `19 <./src/p0019.js>`__ - ☒ `20 <./src/p0020.js>`__ - ☒ `22 <./src/p0022.js>`__ - ☒ `27 <./src/p0027.js>`__