본문 바로가기
python-algorithm

leetcode 2427. Number of Common Factors

by 무적김두칠 2023. 1. 10.

https://leetcode.com/problems/number-of-common-factors/description/

 

Number of Common Factors - LeetCode

Number of Common Factors - Given two positive integers a and b, return the number of common factors of a and b. An integer x is a common factor of a and b if x divides both a and b.   Example 1: Input: a = 12, b = 6 Output: 4 Explanation: The common facto

leetcode.com

 

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(1int(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

반응형

댓글