# Resource recommendation to help with deriving mathematical formulas

### Lily ・2 min read

Hi everyone,

I am a newbie to software development and lately I've been trying to improve on my problem solving skills and one common theme that stood out to me was my inability to derive mathematical formulas that will help with my programming logic. I am not sure if this makes sense, but let me give you a few examples, and hoping to get some feedback on how to improve on this skill?

##### Problem 1: Multiples of 3 and 5

*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.*

I wrote the following function to solve it, which still have lots of room for improvement and need to make it DRY, but I was hoping I could come up with a mathematical formula to solve it instead of using brute force.

```
function sum(number, max) {
let i = 1;
let result = 0;
let sum = 0;
while (result < max) {
result = number * i++;
if(result < max) {
sum = sum + result;
}
}
return sum;
}
console.log(sum(3, 1000) + sum(5, 1000));
```

##### Problem 2: Calculate sum up to n

*Write a function that calculates sum up to n*

I didn't write this, but someone wrote a formula for it involving n, and I have no idea how he was able to derive this.

```
function sum(n) {
return n * (n+1)/2;
}
```

which does the same thing as this:

```
function sum(n) {
let total = 0;
for (let i=0; i <= n; i++) {
total += i;
}
return total;
}
```

Hopefully this clearly explains my question and any recommendation would be greatly appreciated!

Thank you,

Both of these problems require some background knowledge of summations. You can read more about common summations formulas here.

Also, your solution for the first problem is incorrect. This problem is a bit tricky as it requires the use of the inclusion–exclusion principle in order to prevent counting multiples of 15 twice. This stack exchange answer shows the formula used in this solution.

The correct solution for problem #1 would be as follows

Hello Alexander!!! Thank you so much the reply!!!! No wonder I couldn't get the answer right for the first problem, but I was confused because it worked when the max was 10! I will read up on all the links you provided and get a better understanding on how to solve these types of problems!

Thank you thank you!!

Here are a list of explained problems in Javascript w3resource.com/javascript-exercise.... They are not strict formulas but you can learn a lot.

Thank you so much Arber!!!!