Factors of an integer: Difference between revisions
Content added Content deleted
mNo edit summary |
(Faster implementation in scala) |
||
Line 3,084: | Line 3,084: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
<lang Scala> |
<lang Scala> |
||
Brute force approach: |
|||
def factors(num: Int) = { |
def factors(num: Int) = { |
||
(1 to num).filter { divisor => |
(1 to num).filter { divisor => |
||
num % divisor == 0 |
num % divisor == 0 |
||
} |
} |
||
⚫ | |||
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 |
|||
} |
|||
⚫ | |||
=={{header|Scheme}}== |
=={{header|Scheme}}== |