Exercism v3 launches on Sept 1st 2021. Learn more! ๐๐๐

Published at Jul 13 2018
·
3 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!

See this guide for instructions on how to setup your local R environment.

In each problem folder, there is a file named `<exercise_name>.R`

containing a function that returns a `NULL`

value. Place your implementation inside the body of the function.

Inside of RStudio, simply execute the `test_<exercise_name>.R`

script. This can be conveniently done with testthat's `auto_test`

function. Because exercism code and tests are in the same folder, use this same path for both `code_path`

and `test_path`

parameters. On the command-line, you can also run `Rscript test_<exercise_name>.R`

.

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.

```
source("./prime-factors.R")
suppressPackageStartupMessages({
library(testthat)
})
context("prime factors")
test_that("no factors", {
number <- 1
expect_equal(prime_factors(number),
c())
})
test_that("prime number", {
number <- 2
expect_equal(prime_factors(number),
c(2))
})
test_that("square of a prime", {
number <- 9
expect_equal(prime_factors(number),
c(3, 3))
})
test_that("cube of a prime", {
number <- 8
expect_equal(prime_factors(number),
c(2, 2, 2))
})
test_that("product of primes and non-primes", {
number <- 12
expect_equal(prime_factors(number),
c(2, 2, 3))
})
test_that("product of primes", {
number <- 901255
expect_equal(prime_factors(number),
c(5, 17, 23, 461))
})
test_that("factors include a large prime", {
number <- 93819012551
expect_equal(prime_factors(number),
c(11, 9539, 894119))
})
message("All tests passed for exercise: prime-factors")
```

```
prime_factors <- function(number) {
# set start conditions
factors <- c()
f <- 2
# loop through checking factors for even division and append
# [ ] recursion possible as in collatz.R?
while (number > 1) {
if (number %% f == 0) {
factors <- c(factors, f)
number <- number / f
} else {
f <- f + 1
}
}
factors
}
```

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,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

I wonder whether recursion is at all possible here, instead of a loop? I got it working for Collatz Conjecture scripts in R, Python and C, but not here...

Recursion is possible in R... but it doesn't do tail call optimization. What happened when you tried it?

Don't know anymore, sorry. Some errors that I found easier to circumvent by another approach in this case, than trying to apply what worked for Collatz :-(