Published at Oct 01 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!

Make sure you have read the Installing and Running the Tests pages for C++ on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

```
#if defined(EXERCISM_RUN_ALL_TESTS)
```

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C++14 facilities in preference to writing your own low-level algorithms or facilities by hand. CppReference is a wiki reference to the C++ language and standard library. If you are new to C++, but have programmed in C, beware of C traps and pitfalls.

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.

```
#include "prime_factors.h"
#include "test/catch.hpp"
TEST_CASE("_1_yields_empty")
{
const std::vector<int> expected{};
const std::vector<int> actual{prime_factors::of(1)};
REQUIRE(expected == actual);
}
#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("_2_yields_2")
{
const std::vector<int> expected{2};
const std::vector<int> actual{prime_factors::of(2)};
REQUIRE(expected == actual);
}
TEST_CASE("_3_yields_3")
{
const std::vector<int> expected{3};
const std::vector<int> actual{prime_factors::of(3)};
REQUIRE(expected == actual);
}
TEST_CASE("_4_yields_2_2")
{
const std::vector<int> expected{2, 2};
const std::vector<int> actual{prime_factors::of(4)};
REQUIRE(expected == actual);
}
TEST_CASE("_6_yields_2_3")
{
const std::vector<int> expected{2, 3};
const std::vector<int> actual{prime_factors::of(6)};
REQUIRE(expected == actual);
}
TEST_CASE("_8_yields_2_2_2")
{
const std::vector<int> expected{2, 2, 2};
const std::vector<int> actual{prime_factors::of(8)};
REQUIRE(expected == actual);
}
TEST_CASE("_9_yields_3_3")
{
const std::vector<int> expected{3, 3};
const std::vector<int> actual{prime_factors::of(9)};
REQUIRE(expected == actual);
}
TEST_CASE("_27_yields_3_3_3")
{
const std::vector<int> expected{3, 3, 3};
const std::vector<int> actual{prime_factors::of(27)};
REQUIRE(expected == actual);
}
TEST_CASE("_625_yields_5_5_5_5")
{
const std::vector<int> expected{5, 5, 5, 5};
const std::vector<int> actual{prime_factors::of(625)};
REQUIRE(expected == actual);
}
TEST_CASE("_901255_yields_5_17_23_461")
{
const std::vector<int> expected{5, 17, 23, 461};
const std::vector<int> actual{prime_factors::of(901255)};
REQUIRE(expected == actual);
}
#endif
```

```
#include "prime_factors.h"
#include <cmath>
namespace prime_factors {
std::vector<int> of( int value )
{
if( value == 1 )
{
return {};
}
std::vector<int> factors;
while( value % 2 == 0 )
{
factors.push_back( 2 );
value = value / 2;
}
for( int i = 3; i <= value; i += 2 )
{
while( value % i == 0 )
{
factors.push_back( i );
value = value / i;
}
}
return factors;
}
} // namespace prime_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,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments