Skip to content

Commit 6488002

Browse files
authored
Update consecutive-numbers-sum.py
1 parent 382672e commit 6488002

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Python/consecutive-numbers-sum.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ def consecutiveNumbersSum(self, N):
1010
# x + x+1 + x+2 + ... + x+l-1 = N = 2^k * M, where k >= 0 and M is odd
1111
# => l*x + (l-1)*l/2 = 2^k * M
1212
# => x = (2^k * M - (l-1)*l/2) / l = 2^k * M/l - (l-1)/2 is a positive integer
13-
# => l could be 2^(k+1) or any odd factor of M (only one of 2^(k+1) and M is possible)
13+
# => l could be 2^(k+1) or any odd factor of M (only one of 2^(k+1) and M must be possible, proof as follows)
1414
# s.t. x = 2^k * M/l - (l-1)/2 is a positive integer, and also unique
15+
#
16+
# proof:
17+
# if l = 2^(k+1) is impossible
18+
# <=> 2^k * M/(2^(k+1)) - (2^(k+1)-1)/2 <= 0
19+
# <=> M - 2^(k+1) <= -1
20+
# <=> 2^(k+1) >= M+1
21+
# <=> 2^k * M/M - (M-1)/2 >= 1, l = M is possible
22+
#
1523
# => the answer is the number of all odd factors of M
1624
# if prime factorization of N is 2^k * p1^a * p2^b * ..
1725
# => answer is the number of all odd factors = (a+1) * (b+1) * ...

0 commit comments

Comments
 (0)