Published at Sep 27 2019
·
0 comments

Instructions

Test suite

Solution

Compute the prime factors of a given natural number.

A prime number is only evenly divisible by itself and 1.

Note that 1 is not a prime number.

What are the prime factors of 60?

- Our first divisor is 2. 2 goes into 60, leaving 30.
- 2 goes into 30, leaving 15.
- 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3.

- 3 goes cleanly into 15, leaving 5.
- 3 does not go cleanly into 5. The next possible factor is 4.
- 4 does not go cleanly into 5. The next possible factor is 5.

- 5 does go cleanly into 5.
- We're left only with 1, so now, we're done.

Our successful divisors in that computation represent the list of prime factors of 60: 2, 2, 3, and 5.

You can check this yourself:

- 2 * 2 * 3 * 5
- = 4 * 15
- = 60
- Success!

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test`

in the exercise directory.

For more detailed info about the Scala track see the help page.

The Prime Factors Kata by Uncle Bob http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
import org.scalatest.{Matchers, FunSuite}
/** @version 1.1.0 */
class PrimeFactorsTest extends FunSuite with Matchers {
test("no factors") {
PrimeFactors.factors(1) should be(List())
}
test("prime number") {
pending
PrimeFactors.factors(2) should be(List(2))
}
test("square of a prime") {
pending
PrimeFactors.factors(9) should be(List(3, 3))
}
test("cube of a prime") {
pending
PrimeFactors.factors(8) should be(List(2, 2, 2))
}
test("product of primes and non-primes") {
pending
PrimeFactors.factors(12) should be(List(2, 2, 3))
}
test("product of primes") {
pending
PrimeFactors.factors(901255) should be(List(5, 17, 23, 461))
}
test("factors include a large prime") {
pending
PrimeFactors.factors(93819012551l) should be(List(11, 9539, 894119))
}
}
```

```
object PrimeFactors {
def factors(value: Long): List[Int] = {
// def primeStream(s: Stream[Int]): Stream[Int] = s.head #:: primeStream(s.tail.filter(v => v % s.head != 0))
// val primes = primeStream(Stream.from(2))
def isPrime(num: Int): Boolean = {
(2 to (num / 3)).filter(x => num % x == 0).isEmpty
}
@annotation.tailrec
def loop(vl: Long, prime: Int, acc: List[Int]): List[Int] = {
if(isPrime(prime)) {
val div = vl / prime.toDouble
div match {
case 1 => prime :: acc
case _ => {
if(vl % prime == 0){
loop(vl / prime,prime, prime :: acc)
}
else loop(vl, prime + 1, acc)
}
}
} else loop(vl,prime + 1, acc)
}
value match {
case 1 => List()
case 2 => List(2)
case _ => loop(value, 2, List()).reverse
}
}
def main(args: Array[String]): Unit = {
println(factors(901255))
}
}
// exercism submit C:\Users\chema\Exercism\scala\prime-factors\src\main\scala\PrimeFactors.scala
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments