---
# System prepended metadata

title: 2427. Number of Common Factors
tags: [Leetcode, Easy, Math]

---

# 2427. Number of Common Factors
###### tags: `Leetcode` `Easy` `Math`
Link: https://leetcode.com/problems/number-of-common-factors/description/
## 思路 $O(sqrt(N))$
找greatest common divisor的方法参考[这里](https://www.baeldung.com/java-greatest-common-divisor)
直接暴力解
## Code
```java=
class Solution {
    public int commonFactors(int a, int b) {
        int ans = 0, hi = gcd(a,b);
        for(int i=1; i<=hi; i++){
            if(a%i==0 && b%i==0) ans++;
        }
        return ans;
    }
    private int gcd(int a, int b){
        if (b==0) return a;
        return gcd(b,a%b);
    }
}
```