Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created May 27, 2025 16:25
Show Gist options
  • Save tatsuyax25/a5e09e27c3eceedb8cdf3fa7d7725ceb to your computer and use it in GitHub Desktop.
Save tatsuyax25/a5e09e27c3eceedb8cdf3fa7d7725ceb to your computer and use it in GitHub Desktop.
You are given positive integers n and m. Define two integers as follows: num1: The sum of all integers in the range [1, n] (both inclusive) that are not divisible by m. num2: The sum of all integers in the range [1, n] (both inclusive) that are div
/**
* @param {number} n
* @param {number} m
* @return {number}
*/
var differenceOfSums = function(n, m) {
let num1 = 0; // Sum of numbers NOT divisible by m
let num2 = 0; // Sum of numbers divisible by m
// Loop through all numbers from 1 to n
for (let i = 1; i <= n; i++) {
if (i % m === 0) {
num2 += i; // If divisible by m, add to num2
} else {
num1 += i; // If NOT divisible by m, add to num1
}
}
return num1 - num2; // Return the difference
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment