-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path829 Consecutive Numbers Sum.py
88 lines (73 loc) · 1.82 KB
/
829 Consecutive Numbers Sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python3
"""
Given a positive integer N, how many ways can we write it as a sum of consecutive
positive integers?
Example 1:
Input: 5
Output: 2
Explanation: 5 = 5 = 2 + 3
Example 2:
Input: 9
Output: 3
Explanation: 9 = 9 = 4 + 5 = 2 + 3 + 4
Example 3:
Input: 15
Output: 4
Explanation: 15 = 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5
Note: 1 <= N <= 10 ^ 9.
"""
class Solution:
def consecutiveNumbersSum(self, N: int) -> int:
"""
Arithmetic Array
math
(x0 + xn) * (xn - x0 + 1) / 2 = N
xn = x0 + k - 1
(2x0 + k - 1) * k / 2 = N
2x0 = 2N / k - k + 1
x0 * k = N - k * (k - 1) / 2
# assure for divisibility
"""
cnt = 0
k = 0
while True:
k += 1
x0k = N - k * (k - 1) // 2
if x0k <= 0 :
break
if x0k % k == 0:
cnt += 1
return cnt
def consecutiveNumbersSum_error(self, N: int) -> int:
"""
Arithmetic Array
math
(x0 + xn) * (xn - x0 + 1) / 2 = N
xn = x0 + k - 1
(2x0 + k - 1) * k / 2 = N
2x0 = 2N / k - k + 1
x0 * k = N - k * (k - 1) / 2
# assure for divisibility
"""
cnt = 0
for k in range(1, int(N ** 0.5)): # error
x0k = N - k * (k - 1) // 2
if x0k % k == 0:
cnt += 1
return cnt
def consecutiveNumbersSum_error(self, N: int) -> int:
"""
factor related
9 / 3 = 3
"""
if N == 1:
return 1
cnt = 0
for i in range(1, N):
d = N // i
r = N % i
if r == 0 and d - i // 2 > 0:
cnt += 1
elif r == 1 and N == (d + d + 1) * i // 2:
cnt += 1
return cnt