Skip to content

Commit 650ee47

Browse files
kamikkelsKerry Russell
and
Kerry Russell
authored
Add Meeus' Julian Easter algorithm (TheAlgorithms#363)
Co-authored-by: Kerry Russell <[email protected]>
1 parent c0517a3 commit 650ee47

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Globalization;
3+
using Algorithms.Other;
4+
using NUnit.Framework;
5+
6+
namespace Algorithms.Tests.Other
7+
{
8+
/// <summary>
9+
/// A class for testing the Meeus's Julian Easter algorithm.
10+
/// </summary>
11+
public static class JulianEasterTest
12+
{
13+
private static readonly JulianCalendar Calendar = new();
14+
15+
[TestCaseSource(nameof(CalculateCases))]
16+
public static void CalculateTest(int year, DateTime expected)
17+
{
18+
var result = JulianEaster.Calculate(year);
19+
20+
Assert.AreEqual(expected, result);
21+
}
22+
23+
private static readonly object[] CalculateCases =
24+
{
25+
new object[] { 1800, new DateTime(1800, 04, 08, Calendar) },
26+
new object[] { 1950, new DateTime(1950, 03, 27, Calendar) },
27+
new object[] { 1991, new DateTime(1991, 03, 25, Calendar) },
28+
new object[] { 2000, new DateTime(2000, 04, 17, Calendar) },
29+
new object[] { 2199, new DateTime(2199, 04, 07, Calendar) }
30+
};
31+
}
32+
}

Algorithms/Other/JulianEaster.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Globalization;
3+
4+
namespace Algorithms.Other
5+
{
6+
/// <summary>
7+
/// Date of Easter calculated with Meeus's Julian algorithm.
8+
/// The algorithm is described in Jean Meeus' <a href="https://archive.org/details/astronomicalalgorithmsjeanmeeus1991/page/n73/mode/2up">Astronomical Algorithms (1991, p. 69)</a>.
9+
/// </summary>
10+
public static class JulianEaster
11+
{
12+
/// <summary>
13+
/// Calculates the date of Easter.
14+
/// </summary>
15+
/// <param name="year">Year to calculate the date of Easter.</param>
16+
/// <returns>Date of Easter as a DateTime.</returns>
17+
public static DateTime Calculate(int year)
18+
{
19+
var a = year % 4;
20+
var b = year % 7;
21+
var c = year % 19;
22+
var d = (19 * c + 15) % 30;
23+
var e = (2 * a + 4 * b - d + 34) % 7;
24+
var month = (int)Math.Floor((d + e + 114) / 31M);
25+
var day = ((d + e + 114) % 31) + 1;
26+
27+
DateTime easter = new(year, month, day, new JulianCalendar());
28+
29+
return easter;
30+
}
31+
}
32+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ find more than one implementation for the same objective but using different alg
191191
* [Gauss Optimization](./Algorithms/Other/GaussOptimization.cs)
192192
* [Decisions Convolutions](./Algorithms/Other/DecisionsConvolutions.cs)
193193
* [Welford's Variance](./Algorithms/Other/WelfordsVariance.cs)
194+
* [Julian Easter](./Algorithms/Other/JulianEaster.cs)
194195
* [Problems](./Algorithms/Problems)
195196
* [Stable Marriage](./Algorithms/Problems/StableMarriage)
196197
* [Gale-Shapley](./Algorithms/Problems/StableMarriage/GaleShapley.cs)

0 commit comments

Comments
 (0)