From 9d4bff26db23fd94cceef7d727b0d4baad8bcbdd Mon Sep 17 00:00:00 2001 From: Matheus Muriel Date: 2022年10月12日 18:36:20 -0300 Subject: [PATCH 1/6] Included Project Euler 19 solution --- Project-Euler/Problem019.js | 34 +++++++++++++++++++++++++++ Project-Euler/test/Problem019.test.js | 8 +++++++ 2 files changed, 42 insertions(+) create mode 100644 Project-Euler/Problem019.js create mode 100644 Project-Euler/test/Problem019.test.js diff --git a/Project-Euler/Problem019.js b/Project-Euler/Problem019.js new file mode 100644 index 0000000000..5bed12083d --- /dev/null +++ b/Project-Euler/Problem019.js @@ -0,0 +1,34 @@ +/* +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 n = 0 + let dow = 2 + const months = [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + + for (let y = 1901; y <= 2000; y++) { + months[1] = 28 + ((y % 4 === 0 && y % 100 !== 0) || y % 400 === 0) + + for (const month of months) { + dow += month % 7 + if (dow % 7 === 0) { + n++ + } + } + } + + return n +} diff --git a/Project-Euler/test/Problem019.test.js b/Project-Euler/test/Problem019.test.js new file mode 100644 index 0000000000..42848d6175 --- /dev/null +++ b/Project-Euler/test/Problem019.test.js @@ -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) + }) +}) From 94c212a4a7e5aa59eb83ce87a6d4a974deb4565a Mon Sep 17 00:00:00 2001 From: Matheus Muriel Date: 2022年10月23日 16:00:13 -0300 Subject: [PATCH 2/6] Fix style --- Project-Euler/Problem019.js | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/Project-Euler/Problem019.js b/Project-Euler/Problem019.js index 5bed12083d..9db444abc8 100644 --- a/Project-Euler/Problem019.js +++ b/Project-Euler/Problem019.js @@ -15,20 +15,40 @@ How many Sundays fell on the first of the month during the twentieth century (1 */ export function countingSundays () { - let n = 0 + 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++) { - months[1] = 28 + ((y % 4 === 0 && y % 100 !== 0) || y % 400 === 0) + // Calculate the number of Days in February this year + if (y % 4 === 0) { + if (y % 100 === 0) { + if (y % 400 === 0) { + // Leap Year (29 Days in February) + months[1] = 29 + } else { + // Not Leap Year (28 Days in February) + months[1] = 28 + } + } else { + // Leap Year (29 Days in February) + months[1] = 29 + } + } else { + // Not Leap Year (28 Days in February) + months[1] = 28 + } for (const month of months) { - dow += month % 7 + dow = dow + (month % 7) + if (dow % 7 === 0) { - n++ + numberOfSundays += 1 } } } - return n + return numberOfSundays } From 61e8c7498c6438d2e3a40df3bb0758f9b714dba4 Mon Sep 17 00:00:00 2001 From: Matheus Muriel Date: 2022年10月24日 11:31:46 -0300 Subject: [PATCH 3/6] use of ternary --- Project-Euler/Problem019.js | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/Project-Euler/Problem019.js b/Project-Euler/Problem019.js index 9db444abc8..df7a940100 100644 --- a/Project-Euler/Problem019.js +++ b/Project-Euler/Problem019.js @@ -23,23 +23,7 @@ export function countingSundays () { for (let y = 1901; y <= 2000; y++) { // Calculate the number of Days in February this year - if (y % 4 === 0) { - if (y % 100 === 0) { - if (y % 400 === 0) { - // Leap Year (29 Days in February) - months[1] = 29 - } else { - // Not Leap Year (28 Days in February) - months[1] = 28 - } - } else { - // Leap Year (29 Days in February) - months[1] = 29 - } - } else { - // Not Leap Year (28 Days in February) - months[1] = 28 - } + months[1] = 28 + ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) for (const month of months) { dow = dow + (month % 7) From 330a42755129a7bc50a6f35d9a769a2e199bc9a0 Mon Sep 17 00:00:00 2001 From: Matheus Muriel Date: 2022年10月24日 11:35:26 -0300 Subject: [PATCH 4/6] Fix style --- Project-Euler/Problem019.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project-Euler/Problem019.js b/Project-Euler/Problem019.js index df7a940100..dffcc454a7 100644 --- a/Project-Euler/Problem019.js +++ b/Project-Euler/Problem019.js @@ -23,7 +23,7 @@ export function countingSundays () { 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) + months[1] = 28 + ((y % 4 === 0 && y % 100 !== 0) || y % 400 === 0) for (const month of months) { dow = dow + (month % 7) From ff7deeee36c6f652e2e6e245c0d757b7ea8d832e Mon Sep 17 00:00:00 2001 From: Matheus Muriel Date: 2022年10月24日 15:01:31 -0300 Subject: [PATCH 5/6] Fix ++ --- Project-Euler/Problem019.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project-Euler/Problem019.js b/Project-Euler/Problem019.js index dffcc454a7..3c4abdabbc 100644 --- a/Project-Euler/Problem019.js +++ b/Project-Euler/Problem019.js @@ -29,7 +29,7 @@ export function countingSundays () { dow = dow + (month % 7) if (dow % 7 === 0) { - numberOfSundays += 1 + numberOfSundays++ } } } From b5cfb1e19aadd40ce9e2f2968dddb5246c20e46b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: 2022年10月25日 11:27:11 +0200 Subject: [PATCH 6/6] Commit to trigger CI --- Project-Euler/Problem019.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Project-Euler/Problem019.js b/Project-Euler/Problem019.js index 3c4abdabbc..43fd9f8a7e 100644 --- a/Project-Euler/Problem019.js +++ b/Project-Euler/Problem019.js @@ -11,7 +11,6 @@ You are given the following information, but you may prefer to do some research * 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 () {

AltStyle によって変換されたページ (->オリジナル) /