From e159e90cf9215ad4f7a65f95193c523b2fa907ee Mon Sep 17 00:00:00 2001 From: Vincent Zalzal Date: Sat, 23 May 2020 18:19:36 -0400 Subject: [PATCH 1/2] Add computus in C++ --- contents/computus/code/c++/gauss_easter.cpp | 57 +++++++++++++++++++++ contents/computus/computus.md | 2 + 2 files changed, 59 insertions(+) create mode 100644 contents/computus/code/c++/gauss_easter.cpp diff --git a/contents/computus/code/c++/gauss_easter.cpp b/contents/computus/code/c++/gauss_easter.cpp new file mode 100644 index 000000000..fbb7c0873 --- /dev/null +++ b/contents/computus/code/c++/gauss_easter.cpp @@ -0,0 +1,57 @@ +#include +#include + +std::string computus(int year, bool servois = false) { + // Year's position on the 19 year metonic cycle + int a = year % 19; + + // Century index + int k = year / 100; + + // Shift of metonic cycle, add a day offset every 300 years + int p = (13 + 8 * k) / 25; + + // Correction for non-observed leap days + int q = k / 4; + + // Correction to starting point of calculation each century + int M = (15 - p + k - q) % 30; + + // Number of days from March 21st until the full moon + int d = (19 * a + M) % 30; + + // Returning if user wants value for Servois' table + if (servois) + return std::to_string((21 + d) % 31); + + // Finding the next Sunday + // Century-based offset in weekly calculation + int N = (4 + k - q) % 7; + + // Correction for leap days + int b = year % 4; + int c = year % 7; + + // Days from d to next Sunday + int e = (2 * b + 4 * c + 6 * d + N) % 7; + + // Historical corrections for April 26 and 25 + if ((d == 29 && e == 6) || (d == 28 && e == 6 && a > 10)) + e = -1; + + // Determination of the correct month for Easter + return 22 + d + e > 31 ? "April " + std::to_string(d + e - 9) + : "March " + std::to_string(22 + d + e); +} + +// Here, we will output the date of the Paschal full moon (using Servois +// notation), and Easter for 2020-2030 +int main() { + std::cout << "The following are the dates of the Paschal full moon (using " + "Servois notation) and the date of Easter for 2020-2030 AD:\n" + "Year\tServois number\tEaster\n"; + + for (int year = 2020; year <= 2030; year++) + std::cout << year << "\t\t" << computus(year, true) << '\t' + << computus(year) << std::endl; +} diff --git a/contents/computus/computus.md b/contents/computus/computus.md index 3b5459e7f..e8e54ba8c 100644 --- a/contents/computus/computus.md +++ b/contents/computus/computus.md @@ -287,6 +287,8 @@ For now, we have the code outputting a tuple of $$d$$ and $$e$$, so users can us [import, lang:"crystal"](code/crystal/gauss_easter.cr) {% sample lang="c" %} [import, lang:"c"](code/c/gauss_easter.c) +{% sample lang="cpp" %} +[import, lang:"cpp"](code/c++/gauss_easter.cpp) {% endmethod %} From d58a65e1b7e4673b07c6c7f8521799057d9200ff Mon Sep 17 00:00:00 2001 From: Vincent Zalzal Date: Sun, 24 May 2020 12:39:04 -0400 Subject: [PATCH 2/2] Add braces --- contents/computus/code/c++/gauss_easter.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/contents/computus/code/c++/gauss_easter.cpp b/contents/computus/code/c++/gauss_easter.cpp index fbb7c0873..0f0c1cf78 100644 --- a/contents/computus/code/c++/gauss_easter.cpp +++ b/contents/computus/code/c++/gauss_easter.cpp @@ -21,8 +21,9 @@ std::string computus(int year, bool servois = false) { int d = (19 * a + M) % 30; // Returning if user wants value for Servois' table - if (servois) + if (servois) { return std::to_string((21 + d) % 31); + } // Finding the next Sunday // Century-based offset in weekly calculation @@ -36,8 +37,9 @@ std::string computus(int year, bool servois = false) { int e = (2 * b + 4 * c + 6 * d + N) % 7; // Historical corrections for April 26 and 25 - if ((d == 29 && e == 6) || (d == 28 && e == 6 && a > 10)) + if ((d == 29 && e == 6) || (d == 28 && e == 6 && a > 10)) { e = -1; + } // Determination of the correct month for Easter return 22 + d + e > 31 ? "April " + std::to_string(d + e - 9) @@ -51,7 +53,8 @@ int main() { "Servois notation) and the date of Easter for 2020-2030 AD:\n" "Year\tServois number\tEaster\n"; - for (int year = 2020; year <= 2030; year++) + for (int year = 2020; year <= 2030; year++) { std::cout << year << "\t\t" << computus(year, true) << '\t' << computus(year) << std::endl; + } }