Ctrl K

Signed Sum by Multiples

Easy
You are given positive integers limit and step. Write down the numbers from 1 to limit inclusive and give each a sign: minus if it is a multiple of step, plus otherwise. Return the value of the resulting sum.
Example 1
limit=8, step=4 → 12

4 and 8 are multiples of 4 and get a minus sign, the rest a plus: 1 + 2 + 3 − 4 + 5 + 6 + 7 − 8 = 12.

Example 2
limit=3, step=7 → 6

None of 1, 2, 3 is a multiple of 7, so the answer is just 1 + 2 + 3 = 6.

Example 3
limit=4, step=1 → -10

Every number is a multiple of 1, so all of them get a minus sign: −10.

Constraints
  • 1 ≤ limit ≤ 1000
  • 1 ≤ step ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Number That Reads Both Ways EasyValue of a Roman Numeral EasyAdd One to a Digit Array EasySum of Binary Strings EasyWhole Square Root Easy
loading Python…
Input
limit=8, step=4
Expected
12

Run checks the first 3 tests in your browser. Submit checks all 96 tests on the server, hidden ones included.