Skip to content

Commit 032e23b

Browse files
committed
feat: add solutions to lc problem: No.0829
No.0829.Consecutive Numbers Sum
1 parent d7ce7a4 commit 032e23b

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

solution/0800-0899/0829.Consecutive Numbers Sum/README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,21 @@ tags:
5858

5959
### 方法一:数学推导
6060

61-
连续正整数构成一个等差数列($d=1$)。我们假设等差数列的第一项为 $a$,项数为 $k$, $n=(a+a+k-1)*k/2$,即 $n*2=(a*2+k-1)*k$ ①
61+
连续正整数构成一个公差 $d = 1$ 的等差数列。我们假设等差数列的第一项为 $a$,项数为 $k$,那么 $n = (a + a + k - 1) \times k / 2$,即 $n \times 2 = (a \times 2 + k - 1) \times k$。这里我们可以得出 $k$ 一定能整除 $n \times 2$,并且 $(n \times 2) / k - k + 1$ 一定是偶数
6262

63-
由于是连续正整数, $a>=1$,所以 ① 可以化为 $n*2>=(k+1)*k$,即 $k*(k+1)<=n*2$ ②
63+
由于 $a \geq 1$,所以 $n \times 2 = (a \times 2 + k - 1) \times k \geq k \times (k + 1)$
6464

65-
因此,$k$ 的范围需要满足 $k>=1$ 并且 $k*(k+1)<=n*2$。另外,我们从 ① 式可以发现,$k$ 必须能整除 $n*2$。
65+
综上,我们可以得出:
6666

67-
综上,我们枚举 $k$,累加满足条件的 $k$ 的个数即可。
67+
1. $k$ 一定能整除 $n \times 2$;
68+
2. $k \times (k + 1) \leq n \times 2$;
69+
3. $(n \times 2) / k - k + 1$ 一定是偶数。
6870

69-
时间复杂度 $O(\sqrt{n})$。
71+
我们从 $k = 1$ 开始枚举,当 $k \times (k + 1) > n \times 2$ 时,我们可以结束枚举。在枚举的过程中,我们判断 $k$ 是否能整除 $n \times 2$,并且 $(n \times 2) / k - k + 1$ 是否是偶数,如果是则满足条件,答案加一。
72+
73+
枚举结束后,返回答案即可。
74+
75+
时间复杂度 $O(\sqrt{n})$,其中 $n$ 为给定的正整数。空间复杂度 $O(1)$。
7076

7177
<!-- tabs:start -->
7278

@@ -135,6 +141,21 @@ func consecutiveNumbersSum(n int) int {
135141
}
136142
```
137143

144+
#### TypeScript
145+
146+
```ts
147+
function consecutiveNumbersSum(n: number): number {
148+
let ans = 0;
149+
n <<= 1;
150+
for (let k = 1; k * (k + 1) <= n; ++k) {
151+
if (n % k === 0 && (Math.floor(n / k) + 1 - k) % 2 === 0) {
152+
++ans;
153+
}
154+
}
155+
return ans;
156+
}
157+
```
158+
138159
<!-- tabs:end -->
139160

140161
<!-- solution:end -->

solution/0800-0899/0829.Consecutive Numbers Sum/README_EN.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,23 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: Mathematical Derivation
61+
62+
Consecutive positive integers form an arithmetic sequence with a common difference $d = 1$. Let's assume the first term of the sequence is $a$, and the number of terms is $k$. Then, $n = (a + a + k - 1) \times k / 2$, which simplifies to $n \times 2 = (a \times 2 + k - 1) \times k$. From this, we can deduce that $k$ must divide $n \times 2$ evenly, and $(n \times 2) / k - k + 1$ must be an even number.
63+
64+
Given that $a \geq 1$, it follows that $n \times 2 = (a \times 2 + k - 1) \times k \geq k \times (k + 1)$.
65+
66+
In summary, we can conclude:
67+
68+
1. $k$ must divide $n \times 2$ evenly;
69+
2. $k \times (k + 1) \leq n \times 2$;
70+
3. $(n \times 2) / k - k + 1$ must be an even number.
71+
72+
We start enumerating from $k = 1$, and we can stop when $k \times (k + 1) > n \times 2$. During the enumeration, we check if $k$ divides $n \times 2$ evenly, and if $(n \times 2) / k - k + 1$ is an even number. If both conditions are met, it satisfies the criteria, and we increment the answer by one.
73+
74+
After finishing the enumeration, we return the answer.
75+
76+
The time complexity is $O(\sqrt{n})$, where $n$ is the given positive integer. The space complexity is $O(1)$.
6177

6278
<!-- tabs:start -->
6379

@@ -69,7 +85,7 @@ class Solution:
6985
n <<= 1
7086
ans, k = 0, 1
7187
while k * (k + 1) <= n:
72-
if n % k == 0 and (n // k + 1 - k) % 2 == 0:
88+
if n % k == 0 and (n // k - k + 1) % 2 == 0:
7389
ans += 1
7490
k += 1
7591
return ans
@@ -126,6 +142,21 @@ func consecutiveNumbersSum(n int) int {
126142
}
127143
```
128144

145+
#### TypeScript
146+
147+
```ts
148+
function consecutiveNumbersSum(n: number): number {
149+
let ans = 0;
150+
n <<= 1;
151+
for (let k = 1; k * (k + 1) <= n; ++k) {
152+
if (n % k === 0 && (Math.floor(n / k) + 1 - k) % 2 === 0) {
153+
++ans;
154+
}
155+
}
156+
return ans;
157+
}
158+
```
159+
129160
<!-- tabs:end -->
130161

131162
<!-- solution:end -->

solution/0800-0899/0829.Consecutive Numbers Sum/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def consecutiveNumbersSum(self, n: int) -> int:
33
n <<= 1
44
ans, k = 0, 1
55
while k * (k + 1) <= n:
6-
if n % k == 0 and (n // k + 1 - k) % 2 == 0:
6+
if n % k == 0 and (n // k - k + 1) % 2 == 0:
77
ans += 1
88
k += 1
99
return ans
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function consecutiveNumbersSum(n: number): number {
2+
let ans = 0;
3+
n <<= 1;
4+
for (let k = 1; k * (k + 1) <= n; ++k) {
5+
if (n % k === 0 && (Math.floor(n / k) + 1 - k) % 2 === 0) {
6+
++ans;
7+
}
8+
}
9+
return ans;
10+
}

0 commit comments

Comments
 (0)