Write a Python function to determine whether two integer values are amicable or not.
Understand the Problem
The question asks to create a Python function that determines if two given integer values are amicable numbers. Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. That is, if the sum of the proper divisors (divisors excluding the number itself) of 'a' is equal to 'b' and the sum of the proper divisors of 'b' is equal to 'a', then 'a' and 'b' are amicable.
Answer
```python def sum_proper_divisors(n): s = 0 for i in range(1, n // 2 + 1): if n % i == 0: s += i return s def are_amicable(num1, num2): if num1 <= 0 or num2 <= 0 or num1 == num2: return False sum1 = sum_proper_divisors(num1) sum2 = sum_proper_divisors(num2) return sum1 == num2 and sum2 == num1 ```
Answer for screen readers
def sum_proper_divisors(n):
"""Calculates the sum of proper divisors of a number n."""
if n <= 0:
return 0
sum_divisors = 0
for i in range(1, n // 2 + 1):
if n % i == 0:
sum_divisors += i
return sum_divisors
def are_amicable(num1, num2):
"""Checks if two numbers are amicable."""
if num1 <= 0 or num2 <= 0 or num1 == num2:
return False
sum1 = sum_proper_divisors(num1)
sum2 = sum_proper_divisors(num2)
return sum1 == num2 and sum2 == num1
Steps to Solve
-
Define a helper function to calculate the sum of proper divisors. Create a function
sum_proper_divisors(n)
that calculates the sum of proper divisors of a given numbern
. Proper divisors are all positive divisors ofn
excludingn
itself. -
Iterate through potential divisors. In
sum_proper_divisors(n)
, iterate from 1 up ton // 2 + 1
. Ifi
is a divisor ofn
(i.e.,n % i == 0
), then addi
to the sum of divisors. -
Return the sum of proper divisors. After the loop in
sum_proper_divisors(n)
finishes, return the calculated sum. -
Define the main function to check for amicable numbers. Create a function
are_amicable(num1, num2)
that takes two numbers,num1
andnum2
, as input. -
Handle edge cases. Inside
are_amicable(num1, num2)
, check if the numbers are equal or if either number is non-positive. If any of these conditions are true, returnFalse
because amicable numbers must be distinct positive integers. -
Calculate the sum of proper divisors for both numbers. Call
sum_proper_divisors()
for bothnum1
andnum2
to get their respective sums of proper divisors. Store the results insum1
andsum2
.sum1 = sum_proper_divisors(num1) sum2 = sum_proper_divisors(num2)
-
Check if the sums are equal to the original numbers. Check if
sum1
is equal tonum2
andsum2
is equal tonum1
. If both conditions are true, thennum1
andnum2
are amicable numbers, so returnTrue
. Otherwise, returnFalse
.return sum1 == num2 and sum2 == num1
def sum_proper_divisors(n):
"""Calculates the sum of proper divisors of a number n."""
if n <= 0:
return 0
sum_divisors = 0
for i in range(1, n // 2 + 1):
if n % i == 0:
sum_divisors += i
return sum_divisors
def are_amicable(num1, num2):
"""Checks if two numbers are amicable."""
if num1 <= 0 or num2 <= 0 or num1 == num2:
return False
sum1 = sum_proper_divisors(num1)
sum2 = sum_proper_divisors(num2)
return sum1 == num2 and sum2 == num1
More Information
Amicable numbers are also known as friendly numbers. The smallest pair of amicable numbers is (220, 284). The sum of proper divisors of 220 is 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284, and the sum of proper divisors of 284 is 1 + 2 + 4 + 71 + 142 = 220.
Tips
- Forgetting to exclude the number itself when summing the divisors. The problem defines proper divisors, so the number itself should not be included in the sum.
- Not handling edge cases such as negative numbers, zero, or when the two input numbers are the same. Amicable numbers are defined for distinct positive integers.
- Inefficiently calculating the sum of divisors. Iterating up to
n
is correct but inefficient; it’s sufficient to iterate up ton // 2 + 1
.
AI-generated content may contain errors. Please verify critical information