Published
Edited
Jan 29, 2020
1 star
Insert cell
md`# Project Euler: Problem 1 with Javascript`
Insert cell
md`Problem 1: 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 1000.`
Insert cell
number = 10
Insert cell
md`### Non functional (imperative) solution`
Insert cell
standard = function multiplesOf3and5(number) {
let total = 0;
for (let i = 0; i < number; i++) {
if (i % 3 == 0 || i % 5 == 0) {
total += i;
}
}
return total;
}
Insert cell
standard(number)
Insert cell
md`### Functional (declarative) solution`
Insert cell
functional = function multiplesOf3and5f(number) {
return [...Array(number).keys()] //create array of numbers eg [0,1,2,3,4,5,6,7,8,9,10] if value of variable number is 10
.filter(x => !(x % 3) || !(x % 5)) //create array of plausible numbers - multiples of 3 and 5 [0,3,5
.reduce((acc,cur) => acc + cur);
}
Insert cell
functional(number)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more