-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
[Solution] Project euler challenge 19 with tests #1659
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1ff709e
[Solution] Project euler challenge 19 with tests
ddaniel27 c8aaee4
Merge branch 'TheAlgorithms:master' into master
ddaniel27 4010fb3
Merge branch 'TheAlgorithms:master' into master
ddaniel27 96d1d68
Merge branch 'TheAlgorithms:master' into master
ddaniel27 664da39
update leap year function
ddaniel27 03d7267
Merge branch 'TheAlgorithms:master' into master
ddaniel27 98a6fa4
Remove unnecessary, confusingly placed comments
appgurueu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Problem 19 - Counting Sundays | ||
* | ||
* @see {@link 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-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)? | ||
* | ||
* @author ddaniel27 | ||
*/ | ||
|
||
// Check if a year is a leap year | ||
// A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. | ||
function isLeapYear(year) { | ||
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 | ||
} | ||
|
||
function problem19() { | ||
let sundaysCount = 0 // Count of Sundays | ||
let dayOfWeek = 2 // 1st Jan 1900 was a Monday, so 1st Jan 1901 was a Tuesday | ||
|
||
for (let year = 1901; year <= 2000; year++) { | ||
// From 1901 to 2000 | ||
for (let month = 1; month <= 12; month++) { | ||
// From January to December | ||
if (dayOfWeek === 0) { | ||
// If it's a Sunday (0 is Sunday, 1 is Monday, ..., 6 is Saturday) | ||
sundaysCount++ | ||
} | ||
|
||
let daysInMonth = 31 | ||
if (month === 4 || month === 6 || month === 9 || month === 11) { | ||
// April, June, September, November | ||
daysInMonth = 30 | ||
} else if (month === 2) { | ||
// February | ||
daysInMonth = isLeapYear(year) ? 29 : 28 | ||
} | ||
|
||
dayOfWeek = (dayOfWeek + daysInMonth) % 7 // Calculate the day of the week | ||
} | ||
} | ||
|
||
return sundaysCount | ||
} | ||
|
||
export { problem19 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { problem19 } from '../Problem019.js' | ||
|
||
describe('checking sundays during the twentieth century', () => { | ||
// Project Euler Challenge Check | ||
test('result should be 171', () => { | ||
expect(problem19()).toBe(171) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.