Skip to content

Easter date gauss algorithm #2010

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 5 commits into from
May 19, 2020
Merged
Changes from 4 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
48 changes: 48 additions & 0 deletions other/gauss_easter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
https://en.wikipedia.org/wiki/Computus#Gauss'_Easter_algorithm
"""
import math
from datetime import datetime, timedelta


def gauss_easter(year: int) -> datetime:
"""
Calculation Gregorian easter date for given year

>>> gauss_easter(2007)
datetime.datetime(2007, 4, 8, 0, 0)

>>> gauss_easter(2008)
datetime.datetime(2008, 3, 23, 0, 0)

>>> gauss_easter(2020)
datetime.datetime(2020, 4, 12, 0, 0)

>>> gauss_easter(2021)
datetime.datetime(2021, 4, 4, 0, 0)
"""
a = year % 19
b = year % 4
c = year % 7
k = math.floor(year / 100)
p = math.floor((13 + 8 * k) / 25)
q = k / 4
M = (15 - p + k - q) % 30
N = (4 + k - q) % 7
d = (19 * a + M) % 30
e = (2 * b + 4 * c + 6 * d + N) % 7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single letter variable names are old school and they force the reader to guess a lot to make sense of this algorithm.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be better now.


if d == 29 and e == 6:
return datetime(year, 4, 19)
elif d == 28 and e == 6:
return datetime(year, 4, 18)
else:
return datetime(year, 3, 22) + timedelta(days=int(d + e))


if __name__ == '__main__':
print(f"Easter in 2023 will be {gauss_easter(2023)}")
print(f"Easter in 2021 will be {gauss_easter(2021)}")
print(f"Easter in 2010 was {gauss_easter(2010)}")
print(f"Easter in 1994 was {gauss_easter(1994)}")
print(f"Easter in 2000 was {gauss_easter(2000)}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(f"Easter in 2023 will be {gauss_easter(2023)}")
print(f"Easter in 2021 will be {gauss_easter(2021)}")
print(f"Easter in 2010 was {gauss_easter(2010)}")
print(f"Easter in 1994 was {gauss_easter(1994)}")
print(f"Easter in 2000 was {gauss_easter(2000)}")
for year in (1994, 2000, 2010, 2021, 2023):
tense = "will be" if year > datetime.now().year else "was"
print(f"Easter in {year} {tense} {gauss_easter(year)}")