DEV Community

Lily
Lily

Posted on

Resource recommendation to help with deriving mathematical formulas

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,

Top comments (3)

Collapse
 
afranco20 profile image
Alexander Franco • Edited

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

function sum(factor, max) {
  let n;

  if (max % factor == 0) {
    // we must subtract 1 to find  the
    // number of factors UNDER our max
    n = (max / factor) - 1;
  } else {
    // we need a whole number for the
    // upper limit of our summation
    n = Math.floor(max / factor);
  }

  // formula: c * sum i from i = 1 to k
  return factor * ((n * (n + 1)) / 2);
}

const a = sum(3, 1000);  // sum of multiples of 3
const b = sum(5, 1000);  // sum of multiples of 5
const c = sum(15, 1000); // intersection

// we must subtract the intersection because
// we would be counting multiples of 15 twice
console.log(a + b - c);  // result: 233168
Collapse
 
lilyyanglt profile image
Lily

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!!

Collapse
 
lilyyanglt profile image
Lily

Thank you so much Arber!!!!