Skip to content

Commit 58438ce

Browse files
authored
Add Computus in Rust (#695)
1 parent 1a34a46 commit 58438ce

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
fn computus(year: usize, servois: bool) -> String {
2+
// Year's position on the 19 year metonic cycle
3+
let a = year % 19;
4+
5+
// Century index
6+
let k = year / 100; // NOTE: dividing integers always truncates the result
7+
8+
// Shift of metonic cycle, add a day offset every 300 years
9+
let p = (13 + 8 * k) / 25;
10+
11+
// Correction for non-observed leap days
12+
let q = k / 4;
13+
14+
// Correction to starting point of calculation each century
15+
let m = (15 - p + k - q) % 30;
16+
17+
// Number of days from March 21st until the full moon
18+
let d = (19 * a + m) % 30;
19+
20+
if servois {
21+
return ((21 + d) % 31).to_string();
22+
}
23+
24+
// Finding the next Sunday
25+
// Century-based offset in weekly calculation
26+
let n = (4 + k - q) % 7;
27+
28+
// Correction for leap days
29+
let b = year % 4;
30+
let c = year % 7;
31+
32+
// Days from d to next Sunday
33+
let temp_e = ((2 * b + 4 * c + 6 * d + n) % 7) as isize;
34+
35+
// Historical corrections for April 26 and 25
36+
let e = if (d == 29 && temp_e == 6) || (d == 28 && temp_e == 6 && a > 10) {
37+
-1
38+
} else {
39+
temp_e
40+
};
41+
42+
// Determination of the correct month for Easter
43+
if (22 + d) as isize + e > 31 {
44+
format!("April {}", d as isize + e - 9)
45+
} else {
46+
format!("March {}", 22 + d as isize + e)
47+
}
48+
}
49+
50+
fn main() {
51+
// Here, we will output the date of the Paschal full moon
52+
// (using Servois notation), and Easter for 2020-2030
53+
54+
let years = 2020..=2030;
55+
56+
println!(
57+
"The following are the dates of the Paschal full moon (using \
58+
Servois notation) and the date of Easter for 2020-2030 AD:"
59+
);
60+
println!("Year\tServois number\tEaster");
61+
years.for_each(|year| {
62+
println!(
63+
"{}\t{:<14}\t{}",
64+
year,
65+
computus(year, true),
66+
computus(year, false),
67+
)
68+
});
69+
}

contents/computus/computus.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ For now, we have the code outputting a tuple of $$d$$ and $$e$$, so users can us
285285
[import, lang:"python"](code/python/gauss_easter.py)
286286
{% sample lang="crystal" %}
287287
[import, lang:"crystal"](code/crystal/gauss_easter.cr)
288+
{% sample lang="rust" %}
289+
[import, lang:"rust"](code/rust/gauss_easter.rs)
288290
{% sample lang="ps1" %}
289291
[import, lang:"powershell"](code/powershell/gauss_easter.ps1)
290292
{% sample lang="c" %}

0 commit comments

Comments
 (0)