Skip to content

Commit 00e8b5d

Browse files
authoredOct 17, 2023
feat: add solutions to lc problem: No.2652 (doocs#1827)
No.2652.Sum Multiples
1 parent d7a0a5b commit 00e8b5d

File tree

3 files changed

+188
-12
lines changed

3 files changed

+188
-12
lines changed
 

‎solution/2600-2699/2652.Sum Multiples/README.md

+89-5
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,24 @@
4747

4848
**方法一:枚举**
4949

50-
我们直接枚举 $[1,..n]$ 中的每一个数 $x$,如果 $x$ 能被 $3$$5$$7$ 整除,那么就将 $x$ 累加到答案中。
50+
我们直接枚举 $[1,..n]$ 中的每一个数 $x$,如果 $x$ 能被 $3$, $5$, $7$ 整除,那么就将 $x$ 累加到答案中。
5151

5252
枚举结束后,返回答案即可。
5353

5454
时间复杂度 $O(n)$,其中 $n$ 为题目给定的整数。空间复杂度 $O(1)$。
5555

56+
**方法二:数学(容斥原理)**
57+
58+
我们定义函数 $f(x)$ 表示 $[1,..n]$ 中能被 $x$ 整除的数之和,那么一共有 $m = \left\lfloor \frac{n}{x} \right\rfloor$ 个数能被 $x$ 整除,这些数字分别为 $x$, $2x$, $3x$, $\cdots$, $mx$,构成一个等差数列,首项为 $x$,末项为 $mx$,项数为 $m$,因此 $f(x) = \frac{(x + mx) \times m}{2}$。
59+
60+
根据容斥原理,我们可以得到答案为:
61+
62+
$$
63+
f(3) + f(5) + f(7) - f(3 \times 5) - f(3 \times 7) - f(5 \times 7) + f(3 \times 5 \times 7)
64+
$$
65+
66+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
67+
5668
<!-- tabs:start -->
5769

5870
### **Python3**
@@ -65,6 +77,16 @@ class Solution:
6577
return sum(x for x in range(1, n + 1) if x % 3 == 0 or x % 5 == 0 or x % 7 == 0)
6678
```
6779

80+
```python
81+
class Solution:
82+
def sumOfMultiples(self, n: int) -> int:
83+
def f(x: int) -> int:
84+
m = n // x
85+
return (x + m * x) * m // 2
86+
87+
return f(3) + f(5) + f(7) - f(3 * 5) - f(3 * 7) - f(5 * 7) + f(3 * 5 * 7)
88+
```
89+
6890
### **Java**
6991

7092
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -83,6 +105,22 @@ class Solution {
83105
}
84106
```
85107

108+
```java
109+
class Solution {
110+
private int n;
111+
112+
public int sumOfMultiples(int n) {
113+
this.n = n;
114+
return f(3) + f(5) + f(7) - f(3 * 5) - f(3 * 7) - f(5 * 7) + f(3 * 5 * 7);
115+
}
116+
117+
private int f(int x) {
118+
int m = n / x;
119+
return (x + m * x) * m / 2;
120+
}
121+
}
122+
```
123+
86124
### **C++**
87125

88126
```cpp
@@ -100,6 +138,19 @@ public:
100138
};
101139
```
102140
141+
```cpp
142+
class Solution {
143+
public:
144+
int sumOfMultiples(int n) {
145+
auto f = [&](int x) {
146+
int m = n / x;
147+
return (x + m * x) * m / 2;
148+
};
149+
return f(3) + f(5) + f(7) - f(3 * 5) - f(3 * 7) - f(5 * 7) + f(3 * 5 * 7);
150+
}
151+
};
152+
```
153+
103154
### **Go**
104155

105156
```go
@@ -113,6 +164,16 @@ func sumOfMultiples(n int) (ans int) {
113164
}
114165
```
115166

167+
```go
168+
func sumOfMultiples(n int) int {
169+
f := func(x int) int {
170+
m := n / x
171+
return (x + m*x) * m / 2
172+
}
173+
return f(3) + f(5) + f(7) - f(3*5) - f(3*7) - f(5*7) + f(3*5*7)
174+
}
175+
```
176+
116177
### **TypeScript**
117178

118179
```ts
@@ -127,16 +188,26 @@ function sumOfMultiples(n: number): number {
127188
}
128189
```
129190

191+
```ts
192+
function sumOfMultiples(n: number): number {
193+
const f = (x: number): number => {
194+
const m = Math.floor(n / x);
195+
return ((x + m * x) * m) >> 1;
196+
};
197+
return f(3) + f(5) + f(7) - f(3 * 5) - f(3 * 7) - f(5 * 7) + f(3 * 5 * 7);
198+
}
199+
```
200+
130201
### **Rust**
131202

132203
```rust
133204
impl Solution {
134205
pub fn sum_of_multiples(n: i32) -> i32 {
135206
let mut ans = 0;
136207

137-
for i in 1..=n {
138-
if i % 3 == 0 || i % 5 == 0 || i % 7 == 0 {
139-
ans += i;
208+
for x in 1..=n {
209+
if x % 3 == 0 || x % 5 == 0 || x % 7 == 0 {
210+
ans += x;
140211
}
141212
}
142213

@@ -149,12 +220,25 @@ impl Solution {
149220
impl Solution {
150221
pub fn sum_of_multiples(n: i32) -> i32 {
151222
(1..=n)
152-
.filter(|&i| i % 3 == 0 || i % 5 == 0 || i % 7 == 0)
223+
.filter(|&x| x % 3 == 0 || x % 5 == 0 || x % 7 == 0)
153224
.sum()
154225
}
155226
}
156227
```
157228

229+
```rust
230+
impl Solution {
231+
pub fn sum_of_multiples(n: i32) -> i32 {
232+
fn f(x: i32, n: i32) -> i32 {
233+
let m = n / x;
234+
(x + m * x) * m / 2
235+
}
236+
237+
f(3, n) + f(5, n) + f(7, n) - f(3 * 5, n) - f(3 * 7, n) - f(5 * 7, n) + f(3 * 5 * 7, n)
238+
}
239+
}
240+
```
241+
158242
### **...**
159243

160244
```

‎solution/2600-2699/2652.Sum Multiples/README_EN.md

+96-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@
4242

4343
## Solutions
4444

45+
**Solution 1: Enumeration**
46+
47+
We directly enumerate every number $x$ in $[1,..n]$, and if $x$ is divisible by $3$, $5$, and $7$, we add $x$ to the answer.
48+
49+
After the enumeration, we return the answer.
50+
51+
The time complexity is $O(n)$, where $n$ is the given integer. The space complexity is $O(1)$.
52+
53+
**Solution 2: Mathematics (Inclusion-Exclusion Principle)**
54+
55+
We define a function $f(x)$ to represent the sum of numbers in $[1,..n]$ that are divisible by $x$. There are $m = \left\lfloor \frac{n}{x} \right\rfloor$ numbers that are divisible by $x$, which are $x$, $2x$, $3x$, $\cdots$, $mx$, forming an arithmetic sequence with the first term $x$, the last term $mx$, and the number of terms $m$. Therefore, $f(x) = \frac{(x + mx) \times m}{2}$.
56+
57+
According to the inclusion-exclusion principle, we can obtain the answer as:
58+
59+
$$
60+
f(3) + f(5) + f(7) - f(3 \times 5) - f(3 \times 7) - f(5 \times 7) + f(3 \times 5 \times 7)
61+
$$
62+
63+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
64+
4565
<!-- tabs:start -->
4666

4767
### **Python3**
@@ -52,6 +72,16 @@ class Solution:
5272
return sum(x for x in range(1, n + 1) if x % 3 == 0 or x % 5 == 0 or x % 7 == 0)
5373
```
5474

75+
```python
76+
class Solution:
77+
def sumOfMultiples(self, n: int) -> int:
78+
def f(x: int) -> int:
79+
m = n // x
80+
return (x + m * x) * m // 2
81+
82+
return f(3) + f(5) + f(7) - f(3 * 5) - f(3 * 7) - f(5 * 7) + f(3 * 5 * 7)
83+
```
84+
5585
### **Java**
5686

5787
```java
@@ -68,6 +98,22 @@ class Solution {
6898
}
6999
```
70100

101+
```java
102+
class Solution {
103+
private int n;
104+
105+
public int sumOfMultiples(int n) {
106+
this.n = n;
107+
return f(3) + f(5) + f(7) - f(3 * 5) - f(3 * 7) - f(5 * 7) + f(3 * 5 * 7);
108+
}
109+
110+
private int f(int x) {
111+
int m = n / x;
112+
return (x + m * x) * m / 2;
113+
}
114+
}
115+
```
116+
71117
### **C++**
72118

73119
```cpp
@@ -85,6 +131,19 @@ public:
85131
};
86132
```
87133
134+
```cpp
135+
class Solution {
136+
public:
137+
int sumOfMultiples(int n) {
138+
auto f = [&](int x) {
139+
int m = n / x;
140+
return (x + m * x) * m / 2;
141+
};
142+
return f(3) + f(5) + f(7) - f(3 * 5) - f(3 * 7) - f(5 * 7) + f(3 * 5 * 7);
143+
}
144+
};
145+
```
146+
88147
### **Go**
89148

90149
```go
@@ -98,6 +157,16 @@ func sumOfMultiples(n int) (ans int) {
98157
}
99158
```
100159

160+
```go
161+
func sumOfMultiples(n int) int {
162+
f := func(x int) int {
163+
m := n / x
164+
return (x + m*x) * m / 2
165+
}
166+
return f(3) + f(5) + f(7) - f(3*5) - f(3*7) - f(5*7) + f(3*5*7)
167+
}
168+
```
169+
101170
### **TypeScript**
102171

103172
```ts
@@ -112,16 +181,26 @@ function sumOfMultiples(n: number): number {
112181
}
113182
```
114183

184+
```ts
185+
function sumOfMultiples(n: number): number {
186+
const f = (x: number): number => {
187+
const m = Math.floor(n / x);
188+
return ((x + m * x) * m) >> 1;
189+
};
190+
return f(3) + f(5) + f(7) - f(3 * 5) - f(3 * 7) - f(5 * 7) + f(3 * 5 * 7);
191+
}
192+
```
193+
115194
### **Rust**
116195

117196
```rust
118197
impl Solution {
119198
pub fn sum_of_multiples(n: i32) -> i32 {
120199
let mut ans = 0;
121200

122-
for i in 1..=n {
123-
if i % 3 == 0 || i % 5 == 0 || i % 7 == 0 {
124-
ans += i;
201+
for x in 1..=n {
202+
if x % 3 == 0 || x % 5 == 0 || x % 7 == 0 {
203+
ans += x;
125204
}
126205
}
127206

@@ -134,12 +213,25 @@ impl Solution {
134213
impl Solution {
135214
pub fn sum_of_multiples(n: i32) -> i32 {
136215
(1..=n)
137-
.filter(|&i| i % 3 == 0 || i % 5 == 0 || i % 7 == 0)
216+
.filter(|&x| x % 3 == 0 || x % 5 == 0 || x % 7 == 0)
138217
.sum()
139218
}
140219
}
141220
```
142221

222+
```rust
223+
impl Solution {
224+
pub fn sum_of_multiples(n: i32) -> i32 {
225+
fn f(x: i32, n: i32) -> i32 {
226+
let m = n / x;
227+
(x + m * x) * m / 2
228+
}
229+
230+
f(3, n) + f(5, n) + f(7, n) - f(3 * 5, n) - f(3 * 7, n) - f(5 * 7, n) + f(3 * 5 * 7, n)
231+
}
232+
}
233+
```
234+
143235
### **...**
144236

145237
```

‎solution/2600-2699/2652.Sum Multiples/Solution.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ impl Solution {
22
pub fn sum_of_multiples(n: i32) -> i32 {
33
let mut ans = 0;
44

5-
for i in 1..=n {
6-
if i % 3 == 0 || i % 5 == 0 || i % 7 == 0 {
7-
ans += i;
5+
for x in 1..=n {
6+
if x % 3 == 0 || x % 5 == 0 || x % 7 == 0 {
7+
ans += x;
88
}
99
}
1010

0 commit comments

Comments
 (0)
Please sign in to comment.