Skip to content

Commit 6da42c8

Browse files
committed
feat: add solutions to lc problem: No.1390
No.1390: Four Divisors
1 parent 910f50d commit 6da42c8

File tree

7 files changed

+364
-2
lines changed

7 files changed

+364
-2
lines changed

solution/1300-1399/1390.Four Divisors/README.md

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,151 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:因数分解**
52+
53+
我们可以对每个数进行因数分解,如果因数的个数为 $4$ 个,那么这个数就是符合题意的数,我们将其因数累加到答案中即可。
54+
55+
时间复杂度 $O(n \times \sqrt{n})$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**
5460

5561
<!-- 这里可写当前语言的特殊实现逻辑 -->
5662

5763
```python
58-
64+
class Solution:
65+
def sumFourDivisors(self, nums: List[int]) -> int:
66+
def f(x: int) -> int:
67+
i = 2
68+
cnt, s = 2, x + 1
69+
while i <= x // i:
70+
if x % i == 0:
71+
cnt += 1
72+
s += i
73+
if i * i != x:
74+
cnt += 1
75+
s += x // i
76+
i += 1
77+
return s if cnt == 4 else 0
78+
79+
return sum(f(x) for x in nums)
5980
```
6081

6182
### **Java**
6283

6384
<!-- 这里可写当前语言的特殊实现逻辑 -->
6485

6586
```java
87+
class Solution {
88+
public int sumFourDivisors(int[] nums) {
89+
int ans = 0;
90+
for (int x : nums) {
91+
ans += f(x);
92+
}
93+
return ans;
94+
}
95+
96+
private int f(int x) {
97+
int cnt = 2, s = x + 1;
98+
for (int i = 2; i <= x / i; ++i) {
99+
if (x % i == 0) {
100+
++cnt;
101+
s += i;
102+
if (i * i != x) {
103+
++cnt;
104+
s += x / i;
105+
}
106+
}
107+
}
108+
return cnt == 4 ? s : 0;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
int sumFourDivisors(vector<int>& nums) {
119+
int ans = 0;
120+
for (int x : nums) {
121+
ans += f(x);
122+
}
123+
return ans;
124+
}
125+
126+
int f(int x) {
127+
int cnt = 2, s = x + 1;
128+
for (int i = 2; i <= x / i; ++i) {
129+
if (x % i == 0) {
130+
++cnt;
131+
s += i;
132+
if (i * i != x) {
133+
++cnt;
134+
s += x / i;
135+
}
136+
}
137+
}
138+
return cnt == 4 ? s : 0;
139+
}
140+
};
141+
```
142+
143+
### **Go**
144+
145+
```go
146+
func sumFourDivisors(nums []int) (ans int) {
147+
f := func(x int) int {
148+
cnt, s := 2, x+1
149+
for i := 2; i <= x/i; i++ {
150+
if x%i == 0 {
151+
cnt++
152+
s += i
153+
if i*i != x {
154+
cnt++
155+
s += x / i
156+
}
157+
}
158+
}
159+
if cnt == 4 {
160+
return s
161+
}
162+
return 0
163+
}
164+
for _, x := range nums {
165+
ans += f(x)
166+
}
167+
return
168+
}
169+
```
66170

171+
### **TypeScript**
172+
173+
```ts
174+
function sumFourDivisors(nums: number[]): number {
175+
const f = (x: number): number => {
176+
let cnt = 2;
177+
let s = x + 1;
178+
for (let i = 2; i * i <= x; ++i) {
179+
if (x % i === 0) {
180+
++cnt;
181+
s += i;
182+
if (i * i !== x) {
183+
++cnt;
184+
s += Math.floor(x / i);
185+
}
186+
}
187+
}
188+
return cnt === 4 ? s : 0;
189+
};
190+
let ans = 0;
191+
for (const x of nums) {
192+
ans += f(x);
193+
}
194+
return ans;
195+
}
67196
```
68197

69198
### **...**

solution/1300-1399/1390.Four Divisors/README_EN.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,136 @@ The answer is the sum of divisors of 21 only.
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def sumFourDivisors(self, nums: List[int]) -> int:
53+
def f(x: int) -> int:
54+
i = 2
55+
cnt, s = 2, x + 1
56+
while i <= x // i:
57+
if x % i == 0:
58+
cnt += 1
59+
s += i
60+
if i * i != x:
61+
cnt += 1
62+
s += x // i
63+
i += 1
64+
return s if cnt == 4 else 0
65+
66+
return sum(f(x) for x in nums)
5267
```
5368

5469
### **Java**
5570

5671
```java
72+
class Solution {
73+
public int sumFourDivisors(int[] nums) {
74+
int ans = 0;
75+
for (int x : nums) {
76+
ans += f(x);
77+
}
78+
return ans;
79+
}
80+
81+
private int f(int x) {
82+
int cnt = 2, s = x + 1;
83+
for (int i = 2; i <= x / i; ++i) {
84+
if (x % i == 0) {
85+
++cnt;
86+
s += i;
87+
if (i * i != x) {
88+
++cnt;
89+
s += x / i;
90+
}
91+
}
92+
}
93+
return cnt == 4 ? s : 0;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int sumFourDivisors(vector<int>& nums) {
104+
int ans = 0;
105+
for (int x : nums) {
106+
ans += f(x);
107+
}
108+
return ans;
109+
}
110+
111+
int f(int x) {
112+
int cnt = 2, s = x + 1;
113+
for (int i = 2; i <= x / i; ++i) {
114+
if (x % i == 0) {
115+
++cnt;
116+
s += i;
117+
if (i * i != x) {
118+
++cnt;
119+
s += x / i;
120+
}
121+
}
122+
}
123+
return cnt == 4 ? s : 0;
124+
}
125+
};
126+
```
127+
128+
### **Go**
129+
130+
```go
131+
func sumFourDivisors(nums []int) (ans int) {
132+
f := func(x int) int {
133+
cnt, s := 2, x+1
134+
for i := 2; i <= x/i; i++ {
135+
if x%i == 0 {
136+
cnt++
137+
s += i
138+
if i*i != x {
139+
cnt++
140+
s += x / i
141+
}
142+
}
143+
}
144+
if cnt == 4 {
145+
return s
146+
}
147+
return 0
148+
}
149+
for _, x := range nums {
150+
ans += f(x)
151+
}
152+
return
153+
}
154+
```
57155

156+
### **TypeScript**
157+
158+
```ts
159+
function sumFourDivisors(nums: number[]): number {
160+
const f = (x: number): number => {
161+
let cnt = 2;
162+
let s = x + 1;
163+
for (let i = 2; i * i <= x; ++i) {
164+
if (x % i === 0) {
165+
++cnt;
166+
s += i;
167+
if (i * i !== x) {
168+
++cnt;
169+
s += Math.floor(x / i);
170+
}
171+
}
172+
}
173+
return cnt === 4 ? s : 0;
174+
};
175+
let ans = 0;
176+
for (const x of nums) {
177+
ans += f(x);
178+
}
179+
return ans;
180+
}
58181
```
59182

60183
### **...**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int sumFourDivisors(vector<int>& nums) {
4+
int ans = 0;
5+
for (int x : nums) {
6+
ans += f(x);
7+
}
8+
return ans;
9+
}
10+
11+
int f(int x) {
12+
int cnt = 2, s = x + 1;
13+
for (int i = 2; i <= x / i; ++i) {
14+
if (x % i == 0) {
15+
++cnt;
16+
s += i;
17+
if (i * i != x) {
18+
++cnt;
19+
s += x / i;
20+
}
21+
}
22+
}
23+
return cnt == 4 ? s : 0;
24+
}
25+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func sumFourDivisors(nums []int) (ans int) {
2+
f := func(x int) int {
3+
cnt, s := 2, x+1
4+
for i := 2; i <= x/i; i++ {
5+
if x%i == 0 {
6+
cnt++
7+
s += i
8+
if i*i != x {
9+
cnt++
10+
s += x / i
11+
}
12+
}
13+
}
14+
if cnt == 4 {
15+
return s
16+
}
17+
return 0
18+
}
19+
for _, x := range nums {
20+
ans += f(x)
21+
}
22+
return
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int sumFourDivisors(int[] nums) {
3+
int ans = 0;
4+
for (int x : nums) {
5+
ans += f(x);
6+
}
7+
return ans;
8+
}
9+
10+
private int f(int x) {
11+
int cnt = 2, s = x + 1;
12+
for (int i = 2; i <= x / i; ++i) {
13+
if (x % i == 0) {
14+
++cnt;
15+
s += i;
16+
if (i * i != x) {
17+
++cnt;
18+
s += x / i;
19+
}
20+
}
21+
}
22+
return cnt == 4 ? s : 0;
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def sumFourDivisors(self, nums: List[int]) -> int:
3+
def f(x: int) -> int:
4+
i = 2
5+
cnt, s = 2, x + 1
6+
while i <= x // i:
7+
if x % i == 0:
8+
cnt += 1
9+
s += i
10+
if i * i != x:
11+
cnt += 1
12+
s += x // i
13+
i += 1
14+
return s if cnt == 4 else 0
15+
16+
return sum(f(x) for x in nums)

0 commit comments

Comments
 (0)