#ProjectEuler/Python/Problem19.py #Matthew Ellison # Created: 03-13-19 #Modified: 03-28-19 #How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? """ You are given the following information, but you may prefer to do some research for yourself. 1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. """ #Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses """ Copyright (C) 2019 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . """ from Stopwatch import Stopwatch class DAYS: SUNDAY = 0 MONDAY = 1 TUESDAY = 2 WEDNESDAY = 3 THURSDAY = 4 FRIDAY = 5 SATURDAY = 6 NUMBER_OF_DAYS = 7 ERROR = 8 START_YEAR = 1901 #The year we start counting sundays END_YEAR = 2000 #The year we stop counting sundays #Return the day of the week that the date you pass into it is on def getDay(month: int, day: int, year: int) -> DAYS: #Make sure the numebrs are within propper bounds if((month < 1) or (month > 12) or (day < 1) or (day > 31) or (year < 1)): return DAYS.ERROR numDays = 0 #The number of days between 01-01-0001 and the date given currentYear = 1 currentMonth = 1 currentDay = DAYS.SATURDAY day -= 1 #Add the correct number of days for every year while(currentYear < year): if(isLeapYear(currentYear)): numDays += 366 else: numDays += 365 currentYear += 1 #Add the correct number of days for eveyr month while(currentMonth < month): #February if(currentMonth == 2): if(isLeapYear(currentYear)): numDays += 29 else: numDays += 28 elif((currentMonth == 1) or (currentMonth == 3) or (currentMonth == 5) or (currentMonth == 7) or (currentMonth == 8) or (currentMonth == 10) or (currentMonth == 12)): numDays += 31 #For 30 day months else: numDays += 30 currentMonth += 1 #Account for the weird year of 1752 if(year > 1752): numDays -= 11 elif(year == 1752): if(month > 9): numDays -= 11 elif(month == 9): if(day >= 14): numDays -= 11 #Days 3-13 were skipped that year elif((day > 2) and (day < 14)): return DAYS.ERROR #Add the correct number of days for every day numDays += day currentDay += numDays currentDay = currentDay % DAYS.NUMBER_OF_DAYS if(currentDay == DAYS.SUNDAY): return DAYS.SUNDAY elif(currentDay == DAYS.MONDAY): return DAYS.MONDAY elif(currentDay == DAYS.TUESDAY): return DAYS.TUESDAY elif(currentDay == DAYS.WEDNESDAY): return DAYS.WEDNESDAY elif(currentDay == DAYS.THURSDAY): return DAYS.THURSDAY elif(currentDay == DAYS.FRIDAY): return DAYS.FRIDAY elif(currentDay == DAYS.SATURDAY): return DAYS.SATURDAY else: return DAYS.ERROR #Returns true if the year passed to it is a leap year def isLeapYear(year: int) -> bool: if(year < 1): return False elif((year % 100) == 0): #This rule only applies at and after 1800 if(year <= 1700): return True elif((year % 400) == 0): return True elif((year % 4) == 0): return True return False def Problem19(): totalSundays = 0 #Keep track of the number of sundays #Run for all years from start to end for year in range(START_YEAR, END_YEAR + 1): #Run for all months in the year for month in range(1, 13): day = getDay(month, 1, year) if(day == DAYS.ERROR): print("There was an error with the day") return elif(day == DAYS.SUNDAY): totalSundays += 1 #Print the results print("There are " + str(totalSundays) + " Sundays that landed on the first of the month from " + str(START_YEAR) + " to " + str(END_YEAR)) #Run automatically if the script was called stand alone if __name__ == "__main__": timer = Stopwatch() timer.start() Problem19() timer.stop() print("It took " + timer.getString() + " to run this algorithm") """ Results: There are 171 Sundays that landed on the first of the month from 1901 to 2000 It took 724.930 milliseconds to run this algorithm """