Project Euler 1: Multiples of 3 and 5 Solution
1// O(N)2const solution = () => {3 const N = 10004 let res = 05 for (let i = 1; i < N; i++) {6 if (i % 3 === 0 || i % 5 === 0) {7 res += i8 }9 }10 console.log(res)11}1213// O(1)14// Ref for formular: https://math.stackexchange.com/a/930515const solution2 = () => {16 const N = 10001718 const sumDivisibleBy = x => {19 const n = Math.floor((N - 1) / x)20 return (x * (n * (n + 1))) / 221 }2223 const res = sumDivisibleBy(3) + sumDivisibleBy(5) - sumDivisibleBy(15)2425 console.log(res)26}2728solution()29solution2()
Comments
Loading comments...
Tags
projecteuler
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
LeetCode: Binary Trees With Factors
Mar 14, 2021
Previous Post
Project Euler 2: Even Fibonacci numbers
Mar 14, 2021