RosettaCodeData/Task/Proper-divisors/Swift/proper-divisors-2.swift
2023-07-01 13:44:08 -04:00

20 lines
358 B
Swift

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)
}