Avatar of DEVLORD

DEVLORD's solution

to Prime Factors in the C++ Track

Published at Sep 19 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.

Example

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!

Getting Started

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.

Passing the Tests

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++11 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.

Source

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

Submitting Incomplete Solutions

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

prime_factors_test.cpp

#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

prime_factors.h

#ifndef PRIME_FACTORS
#define PRIME_FACTORS
#include <vector>

namespace prime_factors {
std::vector<int> of(int num);
}  // namespace prime_factors

#endif

prime_factors.cpp

#include "prime_factors.h"
#include <cmath>
#include <vector>

std::vector<int> prime_factors::of(int num) {
  std::vector<int> primefactors;
  primefactors.reserve(std::log2(num));
  for (int i = 2; num > 1;) {
    if (num % i == 0) {
      primefactors.push_back(i);
      num /= i;
    } else
      i++;
  }
  return primefactors;
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?