需要帮助检查 scala 代码是否可以变得简洁。找出 n 的所有因数

Need help to check scala code can be made concise. Find all factors of n

下面的实现是使用 scala 找到给定 'n' 的所有因子。这个scala代码可以简洁吗?请注意下面的代码有 O(sqrt(n)).

    @scala.annotation.tailrec
    def helper(n: Int, current: Int, acc: List[Int]): List[Int] = {
      if (current > math.sqrt(n)) acc
      else if (n % current == 0) {
        val a = n / current
        val b = n / a
        helper(n, current + 1, acc :+ a :+ b)
      } else helper(n, current + 1, acc)
    }

    helper(A, 1, List.empty[Int]).sorted.toArray

我不是在寻找以下解决方案,因为这是 O(n) 解决方案。

   def factors(n: Int): List[Int] = {
     (1 to n).filter(n % _ == 0)
   }
 def factors(n: Int): List[Int] = {
     (1 to n).filter(n % _ == 0)
   } 

确实是O(n)。

但是

def factors(n: Int) = 
  (1 to sqrt(n).toInt).filter(n % _ == 0).flatMap { k => Seq(k, n/k) } 

是 O(sqrt(n)) :)