TheAlgorithms-Python

Форк
0
/
gauss_easter.py 
60 строк · 1.9 Кб
1
"""
2
https://en.wikipedia.org/wiki/Computus#Gauss'_Easter_algorithm
3
"""
4

5
import math
6
from datetime import UTC, datetime, timedelta
7

8

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

13
    >>> gauss_easter(2007)
14
    datetime.datetime(2007, 4, 8, 0, 0, tzinfo=datetime.timezone.utc)
15

16
    >>> gauss_easter(2008)
17
    datetime.datetime(2008, 3, 23, 0, 0, tzinfo=datetime.timezone.utc)
18

19
    >>> gauss_easter(2020)
20
    datetime.datetime(2020, 4, 12, 0, 0, tzinfo=datetime.timezone.utc)
21

22
    >>> gauss_easter(2021)
23
    datetime.datetime(2021, 4, 4, 0, 0, tzinfo=datetime.timezone.utc)
24
    """
25
    metonic_cycle = year % 19
26
    julian_leap_year = year % 4
27
    non_leap_year = year % 7
28
    leap_day_inhibits = math.floor(year / 100)
29
    lunar_orbit_correction = math.floor((13 + 8 * leap_day_inhibits) / 25)
30
    leap_day_reinstall_number = leap_day_inhibits / 4
31
    secular_moon_shift = (
32
        15 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number
33
    ) % 30
34
    century_starting_point = (4 + leap_day_inhibits - leap_day_reinstall_number) % 7
35

36
    # days to be added to March 21
37
    days_to_add = (19 * metonic_cycle + secular_moon_shift) % 30
38

39
    # PHM -> Paschal Full Moon
40
    days_from_phm_to_sunday = (
41
        2 * julian_leap_year
42
        + 4 * non_leap_year
43
        + 6 * days_to_add
44
        + century_starting_point
45
    ) % 7
46

47
    if days_to_add == 29 and days_from_phm_to_sunday == 6:
48
        return datetime(year, 4, 19, tzinfo=UTC)
49
    elif days_to_add == 28 and days_from_phm_to_sunday == 6:
50
        return datetime(year, 4, 18, tzinfo=UTC)
51
    else:
52
        return datetime(year, 3, 22, tzinfo=UTC) + timedelta(
53
            days=int(days_to_add + days_from_phm_to_sunday)
54
        )
55

56

57
if __name__ == "__main__":
58
    for year in (1994, 2000, 2010, 2021, 2023, 2032, 2100):
59
        tense = "will be" if year > datetime.now(tz=UTC).year else "was"
60
        print(f"Easter in {year} {tense} {gauss_easter(year)}")
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.