Skip to content

Add Computus in Rust #695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions contents/computus/code/rust/gauss_easter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
fn computus(year: usize, servois: bool) -> String {
// Year's position on the 19 year metonic cycle
let a = year % 19;

// Century index
let k = year / 100; // NOTE: dividing integers always truncates the result

// Shift of metonic cycle, add a day offset every 300 years
let p = (13 + 8 * k) / 25;

// Correction for non-observed leap days
let q = k / 4;

// Correction to starting point of calculation each century
let m = (15 - p + k - q) % 30;

// Number of days from March 21st until the full moon
let d = (19 * a + m) % 30;

if servois {
return ((21 + d) % 31).to_string();
}

// Finding the next Sunday
// Century-based offset in weekly calculation
let n = (4 + k - q) % 7;

// Correction for leap days
let b = year % 4;
let c = year % 7;

// Days from d to next Sunday
let temp_e = ((2 * b + 4 * c + 6 * d + n) % 7) as isize;

// Historical corrections for April 26 and 25
let e = if (d == 29 && temp_e == 6) || (d == 28 && temp_e == 6 && a > 10) {
-1
} else {
temp_e
};

// Determination of the correct month for Easter
if (22 + d) as isize + e > 31 {
format!("April {}", d as isize + e - 9)
} else {
format!("March {}", 22 + d as isize + e)
}
}

fn main() {
// Here, we will output the date of the Paschal full moon
// (using Servois notation), and Easter for 2020-2030

let years = 2020..=2030;

println!(
"The following are the dates of the Paschal full moon (using \
Servois notation) and the date of Easter for 2020-2030 AD:"
);
println!("Year\tServois number\tEaster");
years.for_each(|year| {
println!(
"{}\t{:<14}\t{}",
year,
computus(year, true),
computus(year, false),
)
});
}
2 changes: 2 additions & 0 deletions contents/computus/computus.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ For now, we have the code outputting a tuple of $$d$$ and $$e$$, so users can us
[import, lang:"python"](code/python/gauss_easter.py)
{% sample lang="crystal" %}
[import, lang:"crystal"](code/crystal/gauss_easter.cr)
{% sample lang="rust" %}
[import, lang:"rust"](code/rust/gauss_easter.rs)
{% sample lang="ps1" %}
[import, lang:"powershell"](code/powershell/gauss_easter.ps1)
{% sample lang="c" %}
Expand Down