Scala Tutorial Through Katas: Prime Factors (Easy)

A programming kata is an exercise which helps a programmer hone his skills through practice and repetition.

This article is part of the series “Scala Tutorial Through Katas”. Articles are divided into easy, medium and hard. Beginners should start with easy ones and move towards more complicated once they feel more comfortable programming in Scala.

For the complete list of Scala katas and solutions please visit the index page

The article assumes that the reader is familiar with the basic usage of ScalaTest asserts and knows how to run them from his favorite Scala IDE (ours is IntelliJ IDEA with the Scala plugin).

Tests that prove that the solution is correct are displayed below. Recommended way to solve this kata is to write the implementation for the first test, confirm that it passes and move to the next. Once all of the tests pass, the kata can be considered solved.

One possible solution is provided below the tests. Try to solve the kata by yourself first.

Prime Factors

Compute the prime factors of a given natural number.

[TESTS]

class PrimeFactorsTest extends FlatSpec with Matchers {

  "Prime Factors" must "be List() for 1" in {
    PrimeFactors.result(1) should equal (List[Int]())
  }

  it must "be List(2) for 2" in {
    PrimeFactors.result(2) should equal (List(2))
  }

  it must "be List(3) for 3" in {
    PrimeFactors.result(3) should equal (List(3))
  }

  it must "be List(2, 2) for 4" in {
    PrimeFactors.result(4) should equal (List(2, 2))
  }

  it must "be List(5) for 5" in {
    PrimeFactors.result(5) should equal (List(5))
  }

  it must "be List(2, 3) for 6" in {
    PrimeFactors.result(6) should equal (List(2, 3))
  }

  it must "be List(7) for 7" in {
    PrimeFactors.result(7) should equal (List(7))
  }

  it must "be List(2, 2, 2) for 8" in {
    PrimeFactors.result(8) should equal (List(2, 2, 2))
  }

  it must "be List(3, 3) for 9" in {
    PrimeFactors.result(9) should equal (List(3, 3))
  }

  it must "be List(3, 23) for 69" in {
    PrimeFactors.result(69) should equal (List(3, 23))
  }

  it must "be List(2, 3, 29) for 174" in {
    PrimeFactors.result(174) should equal (List(2, 3, 29))
  }

}

Test code can be found in the GitHub PrimeFactors.scala.

[ONE POSSIBLE SOLUTION]

object PrimeFactors {

  def result(number: Int, list: List[Int] = List()): List[Int] = {
    for(n <- 2 to number if (number % n == 0)) {
      return result(number / n, list :+ n)
    }
    list
  }

}

The solution code can be found in PrimeFactors.scala solution.

What was your solution? Post it as a comment so that we can compare different ways to solve this kata.

3 thoughts on “Scala Tutorial Through Katas: Prime Factors (Easy)

  1. Mr Svensson

    Yours run into problems when factoring products bigger than INT MAX.
    Also, you continue the calculation from 2 every time, instead if continuing from where you found the first (smallest) factor.

    object PrimeFactors {
    def result(number: BigInt, start: BigInt = 2, list: List[BigInt] = Nil): List[BigInt] = {
    Stream.iterate(start)(i => i + 1)
    .takeWhile(n => n <= number)
    .find(n => number % n == 0)
    .map(n => result(number / n, n, list :+ n))
    .getOrElse(list)
    }
    }

    Reply
  2. giriavatar

    this code avoids the inner for loop

    def primeFactors(number: Int): List[Int] = {
    def loop(num: Int, factors: List[Int], divisor: Int): List[Int] = {
    num match {
    case 1 => factors
    case _ =>
    num % divisor match {
    case 0 => loop(num/divisor, divisor :: factors, divisor)
    case _ => loop(num, factors, divisor + 1)
    }
    }
    }
    loop(number, List.empty, 2).reverse
    }

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s