Python Factor Check
## Python Factor Check: Determining if Two Numbers are Mutual Factors
In mathematics, two numbers are considered **mutual factors** (or mutually divisible) if they can divide each other without leaving a remainder. In practical terms, for two positive integers $a$ and $b$, they are mutual factors if $a$ is divisible by $b$ ($a \pmod b = 0$) and $b$ is divisible by $a$ ($b \pmod a = 0$).
In the domain of real positive integers, the only way two numbers can be mutual factors is if they are equal (e.g., 6 and 6). However, in programming, implementing this logic helps us understand conditional statements, modulo operators, and edge-case handling (such as division by zero).
This tutorial demonstrates how to write a robust Python program to check if two numbers are mutual factors.
---
### Mathematical Concept & Logic
To determine if two numbers $a$ and $b$ are mutual factors:
1. $a$ must be divisible by $b$, which means `a % b == 0`.
2. $b$ must be divisible by $a$, which means `b % a == 0`.
In Python, we use the modulo operator (`%`) to find the remainder of a division. If the remainder is `0`, it indicates perfect divisibility.
---
### Code Implementation
#### 1. Basic Implementation
Here is a straightforward Python function to check for mutual factors:
```python
def are_mutual_factors(a, b):
# Check if a is divisible by b AND b is divisible by a
if a % b == 0 and b % a == 0:
return True
else:
return False
# Test the function
num1 = 12
num2 = 6
result = are_mutual_factors(num1, num2)
print(f"Are {num1} and {num2} mutual factors? {result}")
```
**Output:**
```text
Are 12 and 6 mutual factors? False
```
*(Note: In pure mathematics, 12 and 6 are not mutual factors because while 12 is divisible by 6, 6 is not evenly divisible by 12 in integer terms).*
---
### Code Explanation
* **`are_mutual_factors(a, b)`**: A custom function designed to accept two numbers, `a` and `b`, as arguments.
* **`a % b == 0`**: Evaluates whether `a` can be divided by `b` with a remainder of zero.
* **`b % a == 0`**: Evaluates whether `b` can be divided by `a` with a remainder of zero.
* **`and` Operator**: Ensures that **both** conditions must evaluate to `True` for the function to return `True`. If either condition fails, it returns `False`.
---
### Professional Refactoring & Edge Cases
While the basic implementation works for standard non-zero integers, a professional production-ready script must handle edge cases such as **division by zero** (`ZeroDivisionError`) and negative numbers.
#### Optimized & Safe Implementation
```python
def are_mutual_factors_safe(a: int, b: int) -> bool:
"""
Checks if two integers are mutual factors.
Handles ZeroDivisionError and negative integers.
"""
# Division by zero is undefined in mathematics
if a == 0 or b == 0:
return False
# Use absolute values to support negative integers
abs_a, abs_b = abs(a), abs(b)
# Pythonic shorthand return
return abs_a % abs_b == 0 and abs_b % abs_a == 0
# Test Cases
test_cases = [
(6, 6, "Equal positive numbers"),
(12, 6, "Unequal positive numbers"),
(-5, 5, "Negative and positive equal magnitude"),
(0, 5, "Zero boundary check")
]
for x, y, description in test_cases:
res = are_mutual_factors_safe(x, y)
print(f"{description: <40} | are_mutual_factors({x}, {y}) -> {res}")
```
**Output:**
```text
Equal positive numbers | are_mutual_factors(6, 6) -> True
Unequal positive numbers | are_mutual_factors(12, 6) -> False
Negative and positive equal magnitude | are_mutual_factors(-5, 5) -> True
Zero boundary check | are_mutual_factors(0, 5) -> False
```
### Key Takeaways
* Always guard against `ZeroDivisionError` when using the modulo (`%`) or division (`/`) operators in Python.
* Use `abs()` if your mathematical logic needs to treat negative and positive integers uniformly.
* Simplify `if-else` blocks that return booleans by returning the conditional expression directly (e.g., `return condition_a and condition_b`).
YouTip