Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e4f09f4

Browse files
authored
Create Divisible and Non-divisible Sums Difference.py
1 parent 5d2e028 commit e4f09f4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /