https://leetcode.com/problems/number-of-common-factors/description/
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from math import gcd, sqrt
class Solution:
def commonFactors(self, a: int, b: int) -> int:
n = gcd(a, b)
cnt = 0
for i in range(1, int(sqrt(n)) + 1):
if n % i == 0:
if n / i == i:
cnt += 1
else:
cnt += 2
return cnt
|
cs |
1. 최대공약수 구하기
-> math library의 gcd함수 사용
2. 최대공약수의 약수 구하기
for 반복문을 통해 구현
1. Finding the Greatest Common Divisor
-> Use gcd function of math library
2. Finding the Factors of the Greatest Common Divisor
Implemented using a for loop
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 1768. Merge Strings Alternately (0) | 2023.01.11 |
---|---|
leetcode 728. Self Dividing Numbers (0) | 2023.01.10 |
leetcode 2529. Maximum Count of Positive Integer and Negative Integer (0) | 2023.01.10 |
leetcode 2485. Find the Pivot Integer (0) | 2023.01.10 |
leetcode 2418. Sort the People (0) | 2023.01.09 |
댓글