DEV Community

Discussion on: #36 - Sum of Multiples CodeWars Kata (8 kyu)

Collapse
 
snigo profile image
Igor Snitkin

Maybe like this:

function sumMul(n, m) {
  if (m <= n) return "INVALID";
  const l = m - (m % n || n);
  return (n + l) / 2 * l / n;
}
Enter fullscreen mode Exit fullscreen mode

This is based on the fact that the average of [2, 8] is the same as the average of [2, 4, 6, 8], which means that (2 + 8) / 2 === (2 + 4 + 6 + 8) / 4, where 2 + 4 + 6 + 8 is the sum you're looking for.

Good luck!

Collapse
 
cesar__dlr profile image
Cesar Del rio

Great solution bro, I just tested it and it works 🔥