|
| 1 | +''' |
| 2 | +You are given positive integers n and m. |
| 3 | + |
| 4 | +Define two integers as follows: |
| 5 | + |
| 6 | +num1: The sum of all integers in the range [1, n] (both inclusive) that are not divisible by m. |
| 7 | +num2: The sum of all integers in the range [1, n] (both inclusive) that are divisible by m. |
| 8 | +Return the integer num1 - num2. |
| 9 | +''' |
| 10 | + |
| 11 | +----------------------------------------------------------------------- |
| 12 | + |
| 13 | + |
| 14 | +class Solution: |
| 15 | + def differenceOfSums(self, n: int, m: int) -> int: |
| 16 | + n1 = sum(i for i in range(1,n+1) if i%m !=0) |
| 17 | + n2 = sum(i for i in range(1,n+1) if i%m ==0) |
| 18 | + return n1-n2 |
| 19 | + |
| 20 | +----------------------------------------------------------------------- |
| 21 | + |
| 22 | +class Solution: |
| 23 | + def differenceOfSums(self, n: int, m: int) -> int: |
| 24 | + return n*(n+1)//2-m*(n//m)*(n//m+1) |
0 commit comments