Skip to content

Commit 43a89fa

Browse files
authored
Create consecutive-numbers-sum.py
1 parent bce0e50 commit 43a89fa

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

consecutive-numbers-sum.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
"""
3+
@param n: an integer
4+
@return: how many ways can we write it as a sum of consecutive positive integers
5+
"""
6+
def consecutive_numbers_sum(self, n: int) -> int:
7+
# Write your code here
8+
'''
9+
There is a math tricky:
10+
1. K = N
11+
2. K + K + 1 = N -> 2K = N - 1
12+
3. K + K + 1 + K + 2 = N -> 3K = N - 1 - 2
13+
'''
14+
r = 0
15+
i = 1
16+
while n > 0:
17+
if (n % i) == 0:
18+
r += 1
19+
n -= i
20+
i += 1
21+
return r
22+
23+
# medium: https://www.lintcode.com/problem/1439/

0 commit comments

Comments
 (0)