scala way to generate prime factors of a number
问题 What's the scala way to generate the factors of an integer? Here's my take 1: def factorize(x: Int): List[Int] = { def foo(x: Int, a: Int): List[Int] = { if (a > Math.pow(x, 0.5)) return List(x) x % a match { case 0 => a :: foo(x / a, a) case _ => foo(x, a + 1) } } foo(x, 2) } factorize(360) //List(2, 2, 2, 3, 3, 5) Take 2 based on @SpiderPig and @seth-tisue's comments def factorize(x: Int): List[Int] = { def foo(x: Int, a: Int): List[Int] = { (a*a < x, x % a) match { case (true, 0) => a ::