Sum of multiples of 3 or 5 using functional programming I wanted to practice functional programming (fp) without using any library. So I took the 1st problem from project euler:

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.

My solution in fp looks like this:
const isDivisibleBy = i =&gt; number =&gt; i % number === 0;
const selected = criteria =&gt; (...numbers) =&gt; i =&gt; numbers.filter(criteria(i))
    .length &gt; 0;
const getSumOf = conditionFor =&gt; (sum, _, i) =&gt; conditionFor(i) ? sum + i :
    sum;
const solution = Array(1000)
    .fill()
    .reduce(getSumOf(selected(isDivisibleBy)(3, 5)), 0);

I have questions regarding efficiency and parametization:

Is there a more efficient way to write the function selected that is still functional? Because right now the filter functions runs through all elements of numbers - even if it finds an element that is not divisible for the given condition. Also .length &gt; 0 makes it look like a hack. Is there a more declarative way?

I assume getSumOf can't be refactored any more?
This is one of those programming problems where there’s a special trick that can give you a better algorithm, in this case constant-time instead of linear.

If you add the multiples of 3 and the multiples of 5, you’ll count the multiples of 15 twice. So subtract those out. The answer becomes (3+6+9+...+999) + (5+10+15+...+995) - (15+30+45+...+990).

Then, to get all the multiples of m from 1 to N, factor the series m+2·m+... to m· (1 + 2 + ... + ⌊N/m⌋). There’s a simple formula for the series from 1 up to some number, called the Gaussian formula after its discoverer, Carl Friedrich Gauss.  The story goes that he found it as a child, and when his math teacher ordered the whole class to add the numbers from 1 to 100, just to keep them busy so he could take a nap, young Carl shocked him by finding the answer in seconds.  It works on computers, too!  So, substitute this formula into the previous equation to get a constant-time formula for the sum of all multiples of m up to N.  Then substitute that formula into the paragraph above this one, with m = 3, 5 and 15.

Maybe you know all that.  The point of the exercise might have been to practice the specific idioms you used here, like list comprehension and reduction.  Now that you have, you might want to compare an optimal algorithm in functional style.  The last time I posted a better algorithm for a Project Euler problem, someone informed me that the point of Code Review is not to write a better answer myself, so I’ll leave that to you if you feel like it.