Created
March 9, 2020 10:57
-
-
Save abhishekjiitr/eaff27ba12866be6c5b519b41aa43276 to your computer and use it in GitHub Desktop.
Python based Solution to Project Euler Problem 19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment