Skip to content
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

Update Problem001.js #1536

Merged
merged 5 commits into from
Oct 30, 2023
Merged
Changes from 1 commit
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
21 changes: 14 additions & 7 deletions Project-Euler/Problem001.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)=>{
imrahulkb marked this conversation as resolved.
Show resolved Hide resolved
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 }