Given two integers low and high, return all amicable pairs (a, b) with low <= a < b <= high, such that the sum of the proper divisors of a equals b and the sum of the proper divisors of b equals a.
A proper divisor of a positive integer n is a positive divisor of n other than n itself. Return the pairs in ascending order of the first element, with each pair represented as [a, b].
Function Signature
def amicable_pairs(low: int, high: int) -> list[list[int]]:
...
Rules
-
Both numbers of a pair must lie in
[low, high]
. A pair whose smaller number is below
low
, or whose larger number is above
high
, is left out.
-
A perfect number (one whose proper divisors sum to itself, such as 6) does not form a pair with itself, because a pair needs
a < b
.
-
Each pair is
[a, b]
with
a < b
, and the list is sorted by
a
in ascending order. Return an empty list when the range holds no pair.
Constraints
Examples
Input: low = 1, high = 300
Output: [[220, 284]]
The proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, which sum to 284. The proper divisors of 284 are 1, 2, 4, 71 and 142, which sum to 220.
Input: low = 1, high = 1500
Output: [[220, 284], [1184, 1210]]
The proper divisors of 1184 sum to 1210 and the proper divisors of 1210 sum to 1184. The next amicable pair, (2620, 2924), lies above 1500.
Input: low = 250, high = 1200
Output: []
284 is in the range but its partner 220 is below low, and 1184 is in the range but its partner 1210 is above high. Neither pair has both numbers inside [250, 1200].