Proper divisors: Difference between revisions

(add swift)
Line 539:
</pre>
=={{header|Swift}}==
Simple function:
<lang Swift>func properDivsproperDivs1(n: Int) -> [Int] {
var returnList = [Int]()
 
return filter (1 ..< ifn) { (n % i$0 == 0) {}
}</lang>
More efficient function:
<lang Swift>import func Darwin.sqrt
 
func sqrt(x:Int) -> Int { return Int(sqrt(Double(x))) }
 
func properDivs(n: Int) -> [Int] {
if (n == 1) { return [] }
return returnList
}
returnList.append(1)
for (var iresult = 2; i < n; i++[Int]() {
}
if (n % i == 0) {
filter (1 ... sqrt(n)) { n % $0 == 0 } returnList.appendreduce((i)) {
}
returnList result.append($1)
if (n/$1 != $1 && n/$1 != n) { result.append(n/$1) }
}
return returnListsorted(result)
sort(&returnList) {$0 < $1}
}</lang>
return returnList
Rest of the task:
}
<lang Swift>for i in 1...10 {
 
for i in 1...10 {
println("\(i): \(properDivs(i))")
}
 
var highest(num, max) = (0,0)
 
var num = 0
for i in 1...20000 {
}
let newHigh = properDivs(i).count
iflet (newHighcount >= highestproperDivs(i) {.count
if (count > max) { (num, max) = (i, count) }
highest = newHigh
}
}
 
println("\(num): \(highestmax)")</lang>
{{out}}
<pre>1: []
Anonymous user