Sha256: 963bb9155ab1b40d005c381c300638e2e784d733111785da918463179d545264

Contents?: true

Size: 601 Bytes

Versions: 219

Compression:

Stored size: 601 Bytes

Contents

import scala.annotation.tailrec

// Algorithm from - http://fldit-www.cs.uni-dortmund.de/~peter/PS07/HR.pdf
object PrimeFactors {
  def forNumber(n: Long): List[Long] = {
    assert(n >= 1, "Argument must be positive")

    @tailrec
    def primeAcc(n: Long, xs: List[Long]): List[Long] = {
      if (n == 1) xs
      else {
        val p = ld(n)
        primeAcc(n / p, p :: xs)
      }
    }

    primeAcc(n, List()).reverse
  }

  private def ld(n: Long) = ldf(2, n)

  @tailrec
  private def ldf(k: Long, n: Long): Long =
    if (n % k == 0) k
    else if (k * k > n) n
    else ldf(k + 1, n)
}


Version data entries

219 entries across 219 versions & 1 rubygems

Version Path
trackler-2.2.1.2 tracks/scala/exercises/prime-factors/example.scala
trackler-2.2.1.1 tracks/scala/exercises/prime-factors/example.scala
trackler-2.2.1.0 tracks/scala/exercises/prime-factors/example.scala
trackler-2.2.0.6 tracks/scala/exercises/prime-factors/example.scala
trackler-2.2.0.5 tracks/scala/exercises/prime-factors/example.scala
trackler-2.2.0.4 tracks/scala/exercises/prime-factors/example.scala
trackler-2.2.0.3 tracks/scala/exercises/prime-factors/example.scala
trackler-2.2.0.2 tracks/scala/exercises/prime-factors/example.scala
trackler-2.2.0.1 tracks/scala/exercises/prime-factors/example.scala
trackler-2.2.0.0 tracks/scala/exercises/prime-factors/example.scala
trackler-2.1.0.55 tracks/scala/exercises/prime-factors/example.scala
trackler-2.1.0.54 tracks/scala/exercises/prime-factors/example.scala
trackler-2.1.0.53 tracks/scala/exercises/prime-factors/example.scala
trackler-2.1.0.52 tracks/scala/exercises/prime-factors/example.scala
trackler-2.1.0.51 tracks/scala/exercises/prime-factors/example.scala
trackler-2.1.0.50 tracks/scala/exercises/prime-factors/example.scala
trackler-2.1.0.49 tracks/scala/exercises/prime-factors/example.scala
trackler-2.1.0.48 tracks/scala/exercises/prime-factors/example.scala
trackler-2.1.0.47 tracks/scala/exercises/prime-factors/example.scala
trackler-2.1.0.46 tracks/scala/exercises/prime-factors/example.scala