** * Problem 19 - Counting Sundays * @see https://projecteuler.net/problem=19 * * 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-o
()
| 24 | */ |
| 25 | |
| 26 | func Problem19() int { |
| 27 | count := 0 |
| 28 | dayOfWeek := 2 // 1 Jan 1901 was a Tuesday |
| 29 | |
| 30 | for year := 1901; year <= 2000; year++ { |
| 31 | for month := 1; month <= 12; month++ { |
| 32 | if dayOfWeek == 0 { |
| 33 | count++ |
| 34 | } |
| 35 | |
| 36 | daysInMonth := 31 |
| 37 | switch month { |
| 38 | case 4, 6, 9, 11: |
| 39 | daysInMonth = 30 |
| 40 | case 2: |
| 41 | if IsLeapYear(year) { |
| 42 | daysInMonth = 29 |
| 43 | } else { |
| 44 | daysInMonth = 28 |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | dayOfWeek = (dayOfWeek + daysInMonth) % 7 |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return count |
| 53 | } |
| 54 | |
| 55 | func IsLeapYear(year int) bool { |
| 56 | if year%4 == 0 { |