We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent bce0e50 commit 43a89faCopy full SHA for 43a89fa
1 file changed
consecutive-numbers-sum.py
@@ -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