Skip to content

Commit 4313e7e

Browse files
Add computus in C++ (algorithm-archivists#703)
1 parent 2333f3b commit 4313e7e

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
std::string computus(int year, bool servois = false) {
5+
// Year's position on the 19 year metonic cycle
6+
int a = year % 19;
7+
8+
// Century index
9+
int k = year / 100;
10+
11+
// Shift of metonic cycle, add a day offset every 300 years
12+
int p = (13 + 8 * k) / 25;
13+
14+
// Correction for non-observed leap days
15+
int q = k / 4;
16+
17+
// Correction to starting point of calculation each century
18+
int M = (15 - p + k - q) % 30;
19+
20+
// Number of days from March 21st until the full moon
21+
int d = (19 * a + M) % 30;
22+
23+
// Returning if user wants value for Servois' table
24+
if (servois) {
25+
return std::to_string((21 + d) % 31);
26+
}
27+
28+
// Finding the next Sunday
29+
// Century-based offset in weekly calculation
30+
int N = (4 + k - q) % 7;
31+
32+
// Correction for leap days
33+
int b = year % 4;
34+
int c = year % 7;
35+
36+
// Days from d to next Sunday
37+
int e = (2 * b + 4 * c + 6 * d + N) % 7;
38+
39+
// Historical corrections for April 26 and 25
40+
if ((d == 29 && e == 6) || (d == 28 && e == 6 && a > 10)) {
41+
e = -1;
42+
}
43+
44+
// Determination of the correct month for Easter
45+
return 22 + d + e > 31 ? "April " + std::to_string(d + e - 9)
46+
: "March " + std::to_string(22 + d + e);
47+
}
48+
49+
// Here, we will output the date of the Paschal full moon (using Servois
50+
// notation), and Easter for 2020-2030
51+
int main() {
52+
std::cout << "The following are the dates of the Paschal full moon (using "
53+
"Servois notation) and the date of Easter for 2020-2030 AD:\n"
54+
"Year\tServois number\tEaster\n";
55+
56+
for (int year = 2020; year <= 2030; year++) {
57+
std::cout << year << "\t\t" << computus(year, true) << '\t'
58+
<< computus(year) << std::endl;
59+
}
60+
}

contents/computus/computus.md

+2
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ For now, we have the code outputting a tuple of $$d$$ and $$e$$, so users can us
287287
[import, lang:"crystal"](code/crystal/gauss_easter.cr)
288288
{% sample lang="c" %}
289289
[import, lang:"c"](code/c/gauss_easter.c)
290+
{% sample lang="cpp" %}
291+
[import, lang:"cpp"](code/c++/gauss_easter.cpp)
290292
{% endmethod %}
291293

292294

0 commit comments

Comments
 (0)