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 factors of 12 and 6 are 1, 2, 3, 6.
Example 2:
Input: a = 25, b = 30 Output: 2 Explanation: The common factors of 25 and 30 are 1, 5.
Constraints:
1 <= a, b <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the number of common factors involves checking every possible number to see if it divides both input numbers. It is simple to understand and implement. We are essentially testing all possibilities between 1 and the smaller of the two numbers.
Here's how the algorithm would work step-by-step:
def number_of_common_factors_brute_force(number1, number2): number_of_common_factors = 0
smaller_number = min(number1, number2)
for possible_factor in range(1, smaller_number + 1):
# Need to check if the number is a factor of both input numbers
if number1 % possible_factor == 0:
if number2 % possible_factor == 0:
# Found a common factor, so increment the count
number_of_common_factors += 1
return number_of_common_factorsThe most efficient way to find the number of common factors is to realize that any common factor must also be a factor of the smaller number. We can iterate through possible factors up to the smaller number and only check if they divide both numbers evenly.
Here's how the algorithm would work step-by-step:
def number_of_common_factors(number_1, number_2):
smaller_number = min(number_1, number_2)
common_factor_count = 0
# Iterate from 1 to the smaller number to find potential factors.
for possible_factor in range(1, smaller_number + 1):
# Check if the possible factor divides both numbers evenly.
if number_1 % possible_factor == 0:
if number_2 % possible_factor == 0:
# Increment the count if it's a common factor.
common_factor_count += 1
return common_factor_count| Case | How to Handle |
|---|---|
| Either input number is zero | Return 0 as 0 has infinite factors and the only common factor with 0 is the other number when it is also 0. |
| Both input numbers are 1 | Return 1, as 1 is the only common factor. |
| One input number is very large (close to the maximum integer value) | Ensure the algorithm doesn't lead to integer overflow when calculating factors; use long or appropriate data types. |
| Both input numbers are the same large number | The number of common factors will be equal to the number of factors of that single number, calculated without overflow. |
| One number is a multiple of the other (e.g., 12 and 4) | The common factors will be all factors of the smaller number. |
| Both numbers are prime numbers | Return 1, as the only common factor is 1. |
| Both numbers are negative | Take the absolute value of both numbers, as factors are generally considered positive. |
| One number is negative and one is positive | Take the absolute value of both numbers, as factors are generally considered positive. |