Skip to content

solution: Project Euler Problem 19 #1174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Project-Euler/Problem019.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
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.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
*/

export function countingSundays () {
let numberOfSundays = 0
let dow = 2

// The number od days in each month of the year
const months = [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

for (let y = 1901; y <= 2000; y++) {
// Calculate the number of Days in February this year
months[1] = 28 + ((y % 4 === 0 && y % 100 !== 0) || y % 400 === 0)

for (const month of months) {
dow = dow + (month % 7)

if (dow % 7 === 0) {
numberOfSundays++
}
}
}

return numberOfSundays
}
8 changes: 8 additions & 0 deletions Project-Euler/test/Problem019.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { countingSundays } from '../Problem019.js'

describe('checking Counting Sundays', () => {
// Project Euler Condition Check
test('Test Euler Condition', () => {
expect(countingSundays()).toBe(171)
})
})