Skip to content

Commit 707af68

Browse files
committed
feat: add solutions to lc problem: No.2427
No.2427.Number of Common Factors
1 parent f51d868 commit 707af68

File tree

7 files changed

+311
-51
lines changed

7 files changed

+311
-51
lines changed

solution/2400-2499/2427.Number of Common Factors/README.md

Lines changed: 135 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@
3939

4040
**方法一:枚举**
4141

42-
直接枚举 $[1, 1000]$ 中的每个数,判断其是否是 $a$ 和 $b$ 的公因子,如果是,则答案加一。
42+
我们可以先算出 $a$ 和 $b$ 的最大公约数 $g$,然后枚举 $[1,..g]$ 中的每个数,判断其是否是 $g$ 的因子,如果是,则答案加一。
4343

44-
时间复杂度 $O(n)$。本题中 $n = 1000$。
44+
时间复杂度 $O(\min(a, b))$,空间复杂度 $O(1)$。
45+
46+
**方法二:枚举优化**
47+
48+
与方法一类似,我们可以先算出 $a$ 和 $b$ 的最大公约数 $g$,然后枚举最大公约数 $g$ 的所有因子,累加答案。
49+
50+
时间复杂度 $O(\sqrt{\min(a, b)})$,空间复杂度 $O(1)$。
4551

4652
<!-- tabs:start -->
4753

@@ -52,7 +58,21 @@
5258
```python
5359
class Solution:
5460
def commonFactors(self, a: int, b: int) -> int:
55-
return sum(a % i == 0 and b % i == 0 for i in range(1, 1001))
61+
g = gcd(a, b)
62+
return sum(g % x == 0 for x in range(1, g + 1))
63+
```
64+
65+
```python
66+
class Solution:
67+
def commonFactors(self, a: int, b: int) -> int:
68+
g = gcd(a, b)
69+
ans, x = 0, 1
70+
while x * x <= g:
71+
if g % x == 0:
72+
ans += 1
73+
ans += x * x < g
74+
x += 1
75+
return ans
5676
```
5777

5878
### **Java**
@@ -62,14 +82,41 @@ class Solution:
6282
```java
6383
class Solution {
6484
public int commonFactors(int a, int b) {
65-
int ans = 0, n = Math.min(a, b);
66-
for (int i = 1; i <= n; ++i) {
67-
if (a % i == 0 && b % i == 0) {
85+
int g = gcd(a, b);
86+
int ans = 0;
87+
for (int x = 1; x <= g; ++x) {
88+
if (g % x == 0) {
6889
++ans;
6990
}
7091
}
7192
return ans;
7293
}
94+
95+
private int gcd(int a, int b) {
96+
return b == 0 ? a : gcd(b, a % b);
97+
}
98+
}
99+
```
100+
101+
```java
102+
class Solution {
103+
public int commonFactors(int a, int b) {
104+
int g = gcd(a, b);
105+
int ans = 0;
106+
for (int x = 1; x * x <= g; ++x) {
107+
if (g % x == 0) {
108+
++ans;
109+
if (x * x < g) {
110+
++ans;
111+
}
112+
}
113+
}
114+
return ans;
115+
}
116+
117+
private int gcd(int a, int b) {
118+
return b == 0 ? a : gcd(b, a % b);
119+
}
73120
}
74121
```
75122

@@ -79,11 +126,26 @@ class Solution {
79126
class Solution {
80127
public:
81128
int commonFactors(int a, int b) {
129+
int g = gcd(a, b);
82130
int ans = 0;
83-
int n = min(a, b);
84-
for (int i = 1; i <= n; ++i) {
85-
if (a % i == 0 && b % i == 0) {
86-
++ans;
131+
for (int x = 1; x <= g; ++x) {
132+
ans += g % x == 0;
133+
}
134+
return ans;
135+
}
136+
};
137+
```
138+
139+
```cpp
140+
class Solution {
141+
public:
142+
int commonFactors(int a, int b) {
143+
int g = gcd(a, b);
144+
int ans = 0;
145+
for (int x = 1; x * x <= g; ++x) {
146+
if (g % x == 0) {
147+
ans++;
148+
ans += x * x < g;
87149
}
88150
}
89151
return ans;
@@ -94,28 +156,83 @@ public:
94156
### **Go**
95157

96158
```go
97-
func commonFactors(a int, b int) int {
98-
ans := 0
99-
for i := 1; i <= a && i <= b; i++ {
100-
if a%i == 0 && b%i == 0 {
159+
func commonFactors(a int, b int) (ans int) {
160+
g := gcd(a, b)
161+
for x := 1; x <= g; x++ {
162+
if g%x == 0 {
101163
ans++
102164
}
103165
}
104-
return ans
166+
return
167+
}
168+
169+
func gcd(a int, b int) int {
170+
if b == 0 {
171+
return a
172+
}
173+
return gcd(b, a%b)
174+
}
175+
```
176+
177+
```go
178+
func commonFactors(a int, b int) (ans int) {
179+
g := gcd(a, b)
180+
for x := 1; x*x <= g; x++ {
181+
if g%x == 0 {
182+
ans++
183+
if x*x < g {
184+
ans++
185+
}
186+
}
187+
}
188+
return
189+
}
190+
191+
func gcd(a int, b int) int {
192+
if b == 0 {
193+
return a
194+
}
195+
return gcd(b, a%b)
105196
}
106197
```
107198

108199
### **TypeScript**
109200

110201
```ts
111202
function commonFactors(a: number, b: number): number {
112-
const n = Math.min(a, b);
203+
const g = gcd(a, b);
113204
let ans = 0;
114-
for (let i = 1; i <= n; i++) {
115-
if (a % i == 0 && b % i == 0) ans += 1;
205+
for (let x = 1; x <= g; ++x) {
206+
if (g % x === 0) {
207+
++ans;
208+
}
209+
}
210+
return ans;
211+
}
212+
213+
function gcd(a: number, b: number): number {
214+
return b === 0 ? a : gcd(b, a % b);
215+
}
216+
```
217+
218+
```ts
219+
function commonFactors(a: number, b: number): number {
220+
const g = gcd(a, b);
221+
let ans = 0;
222+
for (let x = 1; x * x <= g; ++x) {
223+
if (g % x === 0) {
224+
++ans;
225+
if (x * x < g) {
226+
++ans;
227+
}
228+
}
116229
}
117230
return ans;
118231
}
232+
233+
function gcd(a: number, b: number): number {
234+
return b === 0 ? a : gcd(b, a % b);
235+
}
119236
```
120237

121238
### **...**

solution/2400-2499/2427.Number of Common Factors/README_EN.md

Lines changed: 127 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,63 @@
4141
```python
4242
class Solution:
4343
def commonFactors(self, a: int, b: int) -> int:
44-
return sum(a % i == 0 and b % i == 0 for i in range(1, 1001))
44+
g = gcd(a, b)
45+
return sum(g % x == 0 for x in range(1, g + 1))
46+
```
47+
48+
```python
49+
class Solution:
50+
def commonFactors(self, a: int, b: int) -> int:
51+
g = gcd(a, b)
52+
ans, x = 0, 1
53+
while x * x <= g:
54+
if g % x == 0:
55+
ans += 1
56+
ans += x * x < g
57+
x += 1
58+
return ans
4559
```
4660

4761
### **Java**
4862

4963
```java
5064
class Solution {
5165
public int commonFactors(int a, int b) {
52-
int ans = 0, n = Math.min(a, b);
53-
for (int i = 1; i <= n; ++i) {
54-
if (a % i == 0 && b % i == 0) {
66+
int g = gcd(a, b);
67+
int ans = 0;
68+
for (int x = 1; x <= g; ++x) {
69+
if (g % x == 0) {
70+
++ans;
71+
}
72+
}
73+
return ans;
74+
}
75+
76+
private int gcd(int a, int b) {
77+
return b == 0 ? a : gcd(b, a % b);
78+
}
79+
}
80+
```
81+
82+
```java
83+
class Solution {
84+
public int commonFactors(int a, int b) {
85+
int g = gcd(a, b);
86+
int ans = 0;
87+
for (int x = 1; x * x <= g; ++x) {
88+
if (g % x == 0) {
5589
++ans;
90+
if (x * x < g) {
91+
++ans;
92+
}
5693
}
5794
}
5895
return ans;
5996
}
97+
98+
private int gcd(int a, int b) {
99+
return b == 0 ? a : gcd(b, a % b);
100+
}
60101
}
61102
```
62103

@@ -66,11 +107,26 @@ class Solution {
66107
class Solution {
67108
public:
68109
int commonFactors(int a, int b) {
110+
int g = gcd(a, b);
69111
int ans = 0;
70-
int n = min(a, b);
71-
for (int i = 1; i <= n; ++i) {
72-
if (a % i == 0 && b % i == 0) {
73-
++ans;
112+
for (int x = 1; x <= g; ++x) {
113+
ans += g % x == 0;
114+
}
115+
return ans;
116+
}
117+
};
118+
```
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
int commonFactors(int a, int b) {
124+
int g = gcd(a, b);
125+
int ans = 0;
126+
for (int x = 1; x * x <= g; ++x) {
127+
if (g % x == 0) {
128+
ans++;
129+
ans += x * x < g;
74130
}
75131
}
76132
return ans;
@@ -81,28 +137,83 @@ public:
81137
### **Go**
82138

83139
```go
84-
func commonFactors(a int, b int) int {
85-
ans := 0
86-
for i := 1; i <= a && i <= b; i++ {
87-
if a%i == 0 && b%i == 0 {
140+
func commonFactors(a int, b int) (ans int) {
141+
g := gcd(a, b)
142+
for x := 1; x <= g; x++ {
143+
if g%x == 0 {
144+
ans++
145+
}
146+
}
147+
return
148+
}
149+
150+
func gcd(a int, b int) int {
151+
if b == 0 {
152+
return a
153+
}
154+
return gcd(b, a%b)
155+
}
156+
```
157+
158+
```go
159+
func commonFactors(a int, b int) (ans int) {
160+
g := gcd(a, b)
161+
for x := 1; x*x <= g; x++ {
162+
if g%x == 0 {
88163
ans++
164+
if x*x < g {
165+
ans++
166+
}
89167
}
90168
}
91-
return ans
169+
return
170+
}
171+
172+
func gcd(a int, b int) int {
173+
if b == 0 {
174+
return a
175+
}
176+
return gcd(b, a%b)
92177
}
93178
```
94179

95180
### **TypeScript**
96181

97182
```ts
98183
function commonFactors(a: number, b: number): number {
99-
const n = Math.min(a, b);
184+
const g = gcd(a, b);
100185
let ans = 0;
101-
for (let i = 1; i <= n; i++) {
102-
if (a % i == 0 && b % i == 0) ans += 1;
186+
for (let x = 1; x <= g; ++x) {
187+
if (g % x === 0) {
188+
++ans;
189+
}
103190
}
104191
return ans;
105192
}
193+
194+
function gcd(a: number, b: number): number {
195+
return b === 0 ? a : gcd(b, a % b);
196+
}
197+
```
198+
199+
```ts
200+
function commonFactors(a: number, b: number): number {
201+
const g = gcd(a, b);
202+
let ans = 0;
203+
for (let x = 1; x * x <= g; ++x) {
204+
if (g % x === 0) {
205+
++ans;
206+
if (x * x < g) {
207+
++ans;
208+
}
209+
}
210+
}
211+
return ans;
212+
}
213+
214+
function gcd(a: number, b: number): number {
215+
return b === 0 ? a : gcd(b, a % b);
216+
}
106217
```
107218

108219
### **...**

0 commit comments

Comments
 (0)