Skip to content

Instantly share code, notes, and snippets.

@abhishek-jaisingh
Created March 9, 2020 10:57
Show Gist options
  • Select an option

  • Save abhishek-jaisingh/eaff27ba12866be6c5b519b41aa43276 to your computer and use it in GitHub Desktop.

Select an option

Save abhishek-jaisingh/eaff27ba12866be6c5b519b41aa43276 to your computer and use it in GitHub Desktop.

Revisions

  1. abhishek-jaisingh created this gist Mar 9, 2020.
    40 changes: 40 additions & 0 deletions project-euler-19.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    # 1 Jan 1901 to 31 Dec 2000
    # 1 Jan 1900 Monday => 1 Jan 1901 => Tuesday
    month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

    def is_leap(y):
    return y % 400 == 0 if y % 100 == 0 else y % 4 == 0

    def get_month_days(y, i):
    if is_leap(y) and i == 1:
    return 29
    return month_days[i]

    def get_good_sundays(year):
    res = 0
    diff_years = year - 1900
    leap_years = 0 if (diff_years <= 4 or diff_years == 100) else diff_years / 4
    odd_days = diff_years + leap_years

    curr = 1 + odd_days

    for i in range(0, 12):
    if i != 0:
    curr += get_month_days(year, i-1)
    curr %= 7

    if curr == 0:
    res += 1

    return res


    ans = 0

    YEAR_START = 1901
    YEAR_END = 2000

    for y in range(YEAR_START, YEAR_END+1):
    ans += get_good_sundays(y)

    print(ans)