-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem19.js
57 lines (51 loc) · 1.15 KB
/
problem19.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function isLeap(year) {
if (year % 4 === 0 || year % 400 === 0) {
return 29
}
return 28
}
function getMonths(year) {
return {
'January': 31,
'February': isLeap(year),
'March': 31,
'April': 30,
'May': 31,
'June': 30,
'July': 31,
'August': 31,
'September': 30,
'October': 31,
'November': 30,
'December': 31,
}
}
function problem19() {
// Cycle through 1900 although it's not part of 20th Century. Because we have
// a reference point of 1st Jan 1900, we need to loop through it to keep track
// of the dates
let days = 0
let months = getMonths(1900)
for (let month in months) {
days += months[month]
}
let sundays = 0
for (let year = 1901; year <= 2000; year++) {
months = getMonths(year)
for (let month in months) {
if (days % 7 === 0) {
sundays += 1
}
days += months[month]
}
}
return sundays
}
const startTime = Date.now()
const number = problem19()
const endTime = Date.now()
console.log('number: ', number)
const runningTime = endTime - startTime
console.log(`Running time (s): ${runningTime / 1000}`)
// number: 171
// Running time (s): 0