Two integers 



N


{\displaystyle N}

 and 



M


{\displaystyle M}

 are said to be amicable pairs if 



N
≠
M


{\displaystyle N\neq M}

 and the sum of the proper divisors of 



N


{\displaystyle N}

 (




s
u
m

(

p
r
o
p
D
i
v
s

(
N
)
)


{\displaystyle \mathrm {sum} (\mathrm {propDivs} (N))}

) 



=
M


{\displaystyle =M}

 as well as 




s
u
m

(

p
r
o
p
D
i
v
s

(
M
)
)
=
N


{\displaystyle \mathrm {sum} (\mathrm {propDivs} (M))=N}

.



Example

1184 and 1210 are an amicable pair, with proper divisors:

   1, 2, 4, 8, 16, 32, 37, 74, 148, 296, 592   and 
   1, 2, 5, 10, 11, 22, 55, 110, 121, 242, 605   respectively.


Task

Calculate and show here the Amicable pairs below 20,000; (there are eight).



Related tasks
 Proper divisors
 Abundant, deficient and perfect number classifications
 Aliquot sequence classifications and its amicable classification. 


import func Darwin.sqrt
 
func sqrt(x:Int) -> Int { return Int(sqrt(Double(x))) }
 
func properDivs(n: Int) -> [Int] {
 
    if n == 1 { return [] }
 
    var result = [Int]()
 
    for div in filter (1...sqrt(n), { n % $0 == 0 }) {
 
        result.append(div)
 
        if n/div != div && n/div != n { result.append(n/div) }
    }
 
    return sorted(result)
 
}
 
 
func sumDivs(n:Int) -> Int {
 
    struct Cache { static var sum = [Int:Int]() }
 
    if let sum = Cache.sum[n] { return sum }
 
    let sum = properDivs(n).reduce(0) { $0 + $1 }
 
    Cache.sum[n] = sum
 
    return sum
}
 
func amicable(n:Int, m:Int) -> Bool {
 
    if n == m { return false }
 
    if sumDivs(n) != m || sumDivs(m) != n { return false }
 
    return true
}
 
var pairs = [(Int, Int)]()
 
for n in 1 ..< 20_000 {
    for m in n+1 ... 20_000 {
        if amicable(n, m) {
            pairs.append(n, m)
            println("\(n, m)")
        }
    }
}