From 096f15c4f17296cdbefb8881457dfe8133c3a0c6 Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sat, 14 Oct 2023 19:30:46 +0530 Subject: [PATCH 1/5] Update Problem001.js --- Project-Euler/Problem001.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Project-Euler/Problem001.js b/Project-Euler/Problem001.js index c47c10bd1c..7df2519cf5 100644 --- a/Project-Euler/Problem001.js +++ b/Project-Euler/Problem001.js @@ -4,17 +4,24 @@ Find the sum of all the multiples of 3 or 5 below the provided parameter value number. */ +//nSum function calculates the sum of first n numbers in the table of number num +//here n is denoted as frequency + + +//this method uses the nSum function adds the nSum for 3 and 5 but due to this nSum for 15 has been automatically added twice so it needs to be removed once const multiplesThreeAndFive = (num) => { if (num < 1) throw new Error('No natural numbers exist below 1') + num-=1; + let sum = 0 - let total = 0 - // total for calculating the sum - for (let i = 1; i < num; i++) { - if (i % 3 === 0 || i % 5 === 0) { - total += i + const nSum = (num,frequency)=>{ + return frequency*(frequency+1)*num>>1 } - } - return total + sum += nSum(3,Math.floor(num/3)); + sum += nSum(5,Math.floor(num/5)); + sum -= nSum(15,Math.floor(num/15)); + return sum } + export { multiplesThreeAndFive } From a77bd6d4784f64b5c92ad01d8507d4a69c15bd01 Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sun, 29 Oct 2023 22:09:59 +0530 Subject: [PATCH 2/5] Update Problem001.js --- Project-Euler/Problem001.js | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/Project-Euler/Problem001.js b/Project-Euler/Problem001.js index 7df2519cf5..704d5c2415 100644 --- a/Project-Euler/Problem001.js +++ b/Project-Euler/Problem001.js @@ -1,27 +1,23 @@ // 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. +/* 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. */ -//nSum function calculates the sum of first n numbers in the table of number num -//here n is denoted as frequency +// This method uses the nSum function to add the nSum for 3 and 5. However, it needs to subtract the nSum for 15 once to avoid double counting. +const multiplesThreeAndFive = num => { + if (num < 1) throw new Error('No natural numbers exist below 1') + num -= 1 + let sum = 0 + // The nSum function calculates the sum of the first n numbers in the sequence with a common difference of num. + // Here, n is denoted as frequency. + const nSum = (num, frequency) => (frequency * (frequency + 1) * num) >> 1 -//this method uses the nSum function adds the nSum for 3 and 5 but due to this nSum for 15 has been automatically added twice so it needs to be removed once -const multiplesThreeAndFive = (num) => { - if (num < 1) throw new Error('No natural numbers exist below 1') - num-=1; - let sum = 0 - - const nSum = (num,frequency)=>{ - return frequency*(frequency+1)*num>>1 - } - sum += nSum(3,Math.floor(num/3)); - sum += nSum(5,Math.floor(num/5)); - sum -= nSum(15,Math.floor(num/15)); - return sum + sum += nSum(3, Math.floor(num / 3)) + sum += nSum(5, Math.floor(num / 5)) + sum -= nSum(15, Math.floor(num / 15)) + return sum } - -export { multiplesThreeAndFive } +export {multiplesThreeAndFive} From 2ff146633dcf9d088420cb514cfb095f6c6e8f3e Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sun, 29 Oct 2023 22:15:05 +0530 Subject: [PATCH 3/5] Update Problem001.js --- Project-Euler/Problem001.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project-Euler/Problem001.js b/Project-Euler/Problem001.js index 704d5c2415..81d32bd730 100644 --- a/Project-Euler/Problem001.js +++ b/Project-Euler/Problem001.js @@ -9,11 +9,11 @@ const multiplesThreeAndFive = num => { if (num < 1) throw new Error('No natural numbers exist below 1') num -= 1 let sum = 0 - + // The nSum function calculates the sum of the first n numbers in the sequence with a common difference of num. // Here, n is denoted as frequency. const nSum = (num, frequency) => (frequency * (frequency + 1) * num) >> 1 - + sum += nSum(3, Math.floor(num / 3)) sum += nSum(5, Math.floor(num / 5)) sum -= nSum(15, Math.floor(num / 15)) From b29da45a4b5ffa5020eea86bec3f76c7505a1add Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sun, 29 Oct 2023 22:18:43 +0530 Subject: [PATCH 4/5] Update Problem001.js --- Project-Euler/Problem001.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project-Euler/Problem001.js b/Project-Euler/Problem001.js index 81d32bd730..afe3778869 100644 --- a/Project-Euler/Problem001.js +++ b/Project-Euler/Problem001.js @@ -20,4 +20,4 @@ const multiplesThreeAndFive = num => { return sum } -export {multiplesThreeAndFive} +export { multiplesThreeAndFive } From f29ea97ac06011419f1e54cab7a78d792e5077b9 Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sun, 29 Oct 2023 22:20:51 +0530 Subject: [PATCH 5/5] Update Problem001.js --- Project-Euler/Problem001.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Project-Euler/Problem001.js b/Project-Euler/Problem001.js index afe3778869..a88f912603 100644 --- a/Project-Euler/Problem001.js +++ b/Project-Euler/Problem001.js @@ -5,19 +5,19 @@ Find the sum of all the multiples of 3 or 5 below the provided parameter value n */ // This method uses the nSum function to add the nSum for 3 and 5. However, it needs to subtract the nSum for 15 once to avoid double counting. -const multiplesThreeAndFive = num => { - if (num < 1) throw new Error('No natural numbers exist below 1') - num -= 1 - let sum = 0 - - // The nSum function calculates the sum of the first n numbers in the sequence with a common difference of num. - // Here, n is denoted as frequency. - const nSum = (num, frequency) => (frequency * (frequency + 1) * num) >> 1 - - sum += nSum(3, Math.floor(num / 3)) - sum += nSum(5, Math.floor(num / 5)) - sum -= nSum(15, Math.floor(num / 15)) - return sum +const multiplesThreeAndFive = (num) => { + if (num < 1) throw new Error('No natural numbers exist below 1') + num -= 1 + let sum = 0 + + // The nSum function calculates the sum of the first n numbers in the sequence with a common difference of num. + // Here, n is denoted as frequency. + const nSum = (num, frequency) => (frequency * (frequency + 1) * num) >> 1 + + sum += nSum(3, Math.floor(num / 3)) + sum += nSum(5, Math.floor(num / 5)) + sum -= nSum(15, Math.floor(num / 15)) + return sum } export { multiplesThreeAndFive }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: