Created
April 21, 2012 03:33
-
-
Save underhilllabs/2433637 to your computer and use it in GitHub Desktop.
Project Euler Problem 19
This file contains hidden or 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
#!/usr/bin/python | |
# return true if this year is a leap year | |
def checkLeap(inYear): | |
if(year % 4 == 0): | |
if (year % 100 == 0 and year % 400 != 0): | |
return False | |
else: | |
return True | |
else: | |
return False | |
# current_day | |
day = 0 | |
year = 1900 | |
month = 1 | |
monthDays = [31,28,31,30,31,30,31,31,30,31,30,31] | |
monthDaysLeap = [31,29,31,30,31,30,31,31,30,31,30,31] | |
monthNames = ["Jan","Feb","Mar","April","May","June","July","Aug","Sept","Oct","Nov","Dec"] | |
dayNames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] | |
leapyr = False | |
sundayMonths = 0 | |
# year to start with | |
# use "cal 1 1901" to find day to start with | |
year = 1901 | |
day = 2 | |
while (year <= 2000): | |
# start again in January | |
month = 1 | |
leapYr = checkLeap(year) | |
# set the monthDays array depending on if its a leap year | |
if(leapYr == True): | |
curDays = monthDaysLeap | |
else: | |
curDays = monthDays | |
while(month < 13): | |
# DEBUG | |
#monthStr = "First day of", monthNames[month-1], day, ",", year, "is a", dayNames[day] | |
#print monthStr | |
# add current day plus length of next month | |
# mod with 7 to get first day of month for this month | |
# increment what day next month is | |
day = (day + curDays[month-1]) % 7 | |
# increment the month | |
if (day == 0): | |
print "Found a month that starts with a Sunday in ", month+1, year | |
sundayMonths = sundayMonths + 1 | |
month = month + 1 | |
year = year + 1 | |
print "Found a total of", sundayMonths |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment