From ce6b5f9eed178659347b65315133b72a4cfdd225 Mon Sep 17 00:00:00 2001 From: Victoria Lo Date: Tue, 13 Oct 2020 01:06:48 -0700 Subject: [PATCH 1/5] refactor code to ES6 styling, fixed incorrect code and broken input in Problem 1 --- Project-Euler/Problem1.js | 22 +++++++++++++++------- Project-Euler/Problem2.js | 18 +++++++----------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Project-Euler/Problem1.js b/Project-Euler/Problem1.js index 9c971942ee..7c56951686 100644 --- a/Project-Euler/Problem1.js +++ b/Project-Euler/Problem1.js @@ -3,16 +3,24 @@ Find the sum of all the multiples of 3 or 5 below the provided parameter value number. */ -function multiplesThreeAndFive (num) { - let total = 0 +const readline = require('readline'); + +const multiplesThreeAndFive = (num) => { + let total = 0; // total for calculating the sum - for (let i = 0; i <= num; i++) { + for (let i = 0; i < num; i++) { if (i % 3 === 0 || i % 5 === 0) { - total += i + total += i; } } - return total + return total; } -var num = console.log('Enter a number: ') -console.log(multiplesThreeAndFive(num)) // multiples3_5 function to calculate the sum of multiples of 3 and 5 within num +let rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); +rl.question("Enter a number: ", function(num) { + console.log(multiplesThreeAndFive(num)); // multiples3_5 function to calculate the sum of multiples of 3 and 5 within num + rl.close(); +}); diff --git a/Project-Euler/Problem2.js b/Project-Euler/Problem2.js index cc6f2cea04..5b9ae75e0b 100644 --- a/Project-Euler/Problem2.js +++ b/Project-Euler/Problem2.js @@ -1,16 +1,12 @@ -const SQ5 = 5 ** 0.5 -// Square root of 5 - -const PHI = (1 + SQ5) / 2 -// definition of PHI +const SQ5 = 5 ** 0.5; // Square root of 5 +const PHI = (1 + SQ5) / 2; // definition of PHI // theoretically it should take O(1) constant amount of time as long // arithmetic calculations are considered to be in constant amount of time -function EvenFibonacci (limit) { - const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI)) - const n = Math.floor(highestIndex / 3) +const EvenFibonacci = (limit) => { + const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI)); + const n = Math.floor(highestIndex / 3); return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) - - ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5 + ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5; } -console.log(EvenFibonacci(4e6)) -// Sum of Even Fibonnaci upto 4 Million +console.log(EvenFibonacci(4e6)); // Sum of even Fibonacci upto 4 Million From 3ecae7715c24ec09f20ffc65ecf24bfd3552d226 Mon Sep 17 00:00:00 2001 From: Victoria Lo Date: Tue, 13 Oct 2020 01:09:35 -0700 Subject: [PATCH 2/5] standard styling --- Project-Euler/Problem1.js | 20 ++++++++++---------- Project-Euler/Problem2.js | 12 ++++++------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Project-Euler/Problem1.js b/Project-Euler/Problem1.js index 7c56951686..dcd2dcf26c 100644 --- a/Project-Euler/Problem1.js +++ b/Project-Euler/Problem1.js @@ -3,24 +3,24 @@ Find the sum of all the multiples of 3 or 5 below the provided parameter value number. */ -const readline = require('readline'); +const readline = require('readline') const multiplesThreeAndFive = (num) => { - let total = 0; + let total = 0 // total for calculating the sum for (let i = 0; i < num; i++) { if (i % 3 === 0 || i % 5 === 0) { - total += i; + total += i } } - return total; + return total } -let rl = readline.createInterface({ +const rl = readline.createInterface({ input: process.stdin, output: process.stdout -}); -rl.question("Enter a number: ", function(num) { - console.log(multiplesThreeAndFive(num)); // multiples3_5 function to calculate the sum of multiples of 3 and 5 within num - rl.close(); -}); +}) +rl.question('Enter a number: ', function (num) { + console.log(multiplesThreeAndFive(num)) // multiples3_5 function to calculate the sum of multiples of 3 and 5 within num + rl.close() +}) diff --git a/Project-Euler/Problem2.js b/Project-Euler/Problem2.js index 5b9ae75e0b..2ae04a8e83 100644 --- a/Project-Euler/Problem2.js +++ b/Project-Euler/Problem2.js @@ -1,12 +1,12 @@ -const SQ5 = 5 ** 0.5; // Square root of 5 -const PHI = (1 + SQ5) / 2; // definition of PHI +const SQ5 = 5 ** 0.5 // Square root of 5 +const PHI = (1 + SQ5) / 2 // definition of PHI // theoretically it should take O(1) constant amount of time as long // arithmetic calculations are considered to be in constant amount of time const EvenFibonacci = (limit) => { - const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI)); - const n = Math.floor(highestIndex / 3); + const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI)) + const n = Math.floor(highestIndex / 3) return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) - - ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5; + ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5 } -console.log(EvenFibonacci(4e6)); // Sum of even Fibonacci upto 4 Million +console.log(EvenFibonacci(4e6)) // Sum of even Fibonacci upto 4 Million From fcd7950b47a02eb1567c933408e6dc2c88c48616 Mon Sep 17 00:00:00 2001 From: Victoria Lo Date: Tue, 13 Oct 2020 01:18:57 -0700 Subject: [PATCH 3/5] use standard --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 20ddcf6636..24112b042c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7593,9 +7593,9 @@ } }, "tslib": { - "version": "1.13.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz", - "integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==", + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", "dev": true }, "tunnel-agent": { From d9d93b3ac1a83cc51ee507498b6f386869b0fd7d Mon Sep 17 00:00:00 2001 From: Victoria Lo Date: Tue, 13 Oct 2020 01:25:08 -0700 Subject: [PATCH 4/5] removed changes to Problem 2 --- Project-Euler/Problem2.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Project-Euler/Problem2.js b/Project-Euler/Problem2.js index 2ae04a8e83..cc6f2cea04 100644 --- a/Project-Euler/Problem2.js +++ b/Project-Euler/Problem2.js @@ -1,12 +1,16 @@ -const SQ5 = 5 ** 0.5 // Square root of 5 -const PHI = (1 + SQ5) / 2 // definition of PHI +const SQ5 = 5 ** 0.5 +// Square root of 5 + +const PHI = (1 + SQ5) / 2 +// definition of PHI // theoretically it should take O(1) constant amount of time as long // arithmetic calculations are considered to be in constant amount of time -const EvenFibonacci = (limit) => { +function EvenFibonacci (limit) { const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI)) const n = Math.floor(highestIndex / 3) return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) - ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5 } -console.log(EvenFibonacci(4e6)) // Sum of even Fibonacci upto 4 Million +console.log(EvenFibonacci(4e6)) +// Sum of Even Fibonnaci upto 4 Million From c75008dff65c6708bbaa15b4d69af779e1c7681d Mon Sep 17 00:00:00 2001 From: Victoria Lo Date: Tue, 13 Oct 2020 01:26:34 -0700 Subject: [PATCH 5/5] added URL to euler problem --- Project-Euler/Problem1.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Project-Euler/Problem1.js b/Project-Euler/Problem1.js index dcd2dcf26c..af3582f030 100644 --- a/Project-Euler/Problem1.js +++ b/Project-Euler/Problem1.js @@ -1,3 +1,4 @@ +// https://projecteuler.net/problem=1 /* Multiples of 3 and 5 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below the provided parameter value number.