Skip to content

Instantly share code, notes, and snippets.

@rmariano
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmariano/f660166b34a50d4544af to your computer and use it in GitHub Desktop.
Save rmariano/f660166b34a50d4544af to your computer and use it in GitHub Desktop.
Gregorian Easter
#!/usr/bin/env python3
"""
Sunday 05 April, 2015 - Buenos Aires, Argentina
Python 3.4
Computes the date for the Gregorian Easter by using
Gauss's formula.
"""
from datetime import date
def gauss_easter_algorithm(year):
"""Given a year (int), return the date for easter on that year.
:return: datetime.date with the (year, month, day) for easter."""
MARCH = 3
APRIL = 4
a = year % 19
b = year % 4
c = year % 7
k = year // 100
p = (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
if (d + e) < 10:
easter = (d + e + 22, MARCH)
else:
easter = (d + e - 9, APRIL)
# exceptions
if easter == (26, APRIL):
easter = (19, APRIL)
if easter == (25, APRIL) and d == 28 and e == 6 and a > 10:
easter = (18, APRIL)
return date(year, *easter[::-1])
def interactive():
ready = False
while not ready:
year = input("Enter the year to calculate easter's date (YYYY): ")
try:
if not year.isdigit() or len(year) != 4:
raise ValueError("Invalid year or format: {}".format(year))
year = int(year)
except ValueError as ex:
print(str(ex))
else:
result = gauss_easter_algorithm(year)
easter = result.strftime("%d, %B")
print("For {year} Easter is on: {easter}".format(year=year,
easter=easter))
retry = input("Try again? (y/n): ")
if retry.lower() != 'y':
ready = True
if __name__ == '__main__':
interactive()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment