MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Problem19

Function Problem19

project_euler/problem_19/problem19.go:26–53  ·  view source on GitHub ↗

** * 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

()

Source from the content-addressed store, hash-verified

24 */
25
26func 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
55func IsLeapYear(year int) bool {
56 if year%4 == 0 {

Callers 2

TestProblem19_FuncFunction · 0.85
BenchmarkProblem19_FuncFunction · 0.85

Calls 1

IsLeapYearFunction · 0.85

Tested by 2

TestProblem19_FuncFunction · 0.68
BenchmarkProblem19_FuncFunction · 0.68