Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
088f4b7
commit 9adf24a
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* 1. | ||
* | ||
* Very simple brute force solution that goes | ||
* through all of the numbers between 3..n and | ||
* adds them up when they're divisible by 3 and 5. | ||
*/ | ||
|
||
let n = 1000; | ||
let bruteforce_sum = 0; | ||
|
||
// Start at i = 3, since we're not interested in anything below 3 | ||
for(let i = 3; i < n; i++) { | ||
|
||
bruteforce_sum += i % 3 === 0 || i % 5 === 0 ? i : 0; | ||
|
||
} | ||
|
||
console.log("Bruteforce Solution:", bruteforce_sum); // 233168 | ||
|
||
|
||
|
||
/* 2. | ||
* | ||
* Way more efficient solution using finite arithmetic progression and series | ||
*/ | ||
|
||
function sum(n) { | ||
return (n * (n + 1)) / 2; | ||
} | ||
|
||
const threes = (3 * sum(Math.floor(999/3))); | ||
const fives = (5 * sum(Math.floor(999/5))); | ||
const fifteens = (15 * sum(Math.floor(999/15))); | ||
|
||
const solution = threes + fives - fifteens; | ||
|
||
console.log("Solution:", solution); // 233168 |