JuniorMath

Divisible and Non-divisible Sums Difference

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 divisible by m.

Return the integer num1 - num2.

Example 1
Inputn = 10, m = 3
Output19
Integers not divisible by 3 sum to 37, integers divisible by 3 sum to 18, so the answer is 37 - 18 = 19.
Example 2
Inputn = 5, m = 6
Output15
All integers from 1 to 5 are not divisible by 6, so num1 is 15 and num2 is 0.

Constraints

  • 1 <= n, m <= 1000

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate