Jump to content

Factors of an integer: Difference between revisions

Faster implementation in scala
mNo edit summary
(Faster implementation in scala)
Line 3,084:
=={{header|Scala}}==
<lang Scala>
Brute force approach:
 
def factors(num: Int) = {
(1 to num).filter { divisor =>
num % divisor == 0
}
 
}</lang>
Since factors can't be higher than sqrt(num), the code above can be edited as follows
def factors(num: Int) = {
(1 to sqrt(num)).filter { divisor =>
num % divisor == 0
}
 
}</lang>
 
=={{header|Scheme}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.