usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

C++ Code to List Prime Pactors of a Number - Math Project for Primary Students



What are Prime Factors? | Maths Explanation for C++ Kids

Finding prime factors is all about selecting those factors of a number that are prime.
The prime factors of 36 as an example, are:

2 X 2 X 3 X 3.

In this guide, we'll thoroughly explain prime factorisation and show how to code a C++ algorithm to find prime-factors in a detailed and interactive manner.
This Math exercise and C++ algorithm will help young students understand prime factorization by listing all prime factors of a number.



Step-by-step Guide to Prime Factorisation of Numbers in C++

We'll go about the C++ algorithm to find prime factors in a simple way:

Step 1:

Starting with 2, find the first factor of the number of interest - this factor will definitely be a prime.
Store this factor away.

Step 2:

Divide number in question by found factor.

Step 3:

Using result from Step 2 as the new number in question (number whose prime factors we are trying to find), repeat Step 1.

Step 4:

Continue recursively until we arrive at 1.

Step 5:

We'll use the square-root of number range.


Create a new C++ class file;
Call it ListPrimeFactors.
Type out the adjoining C++ code for listing prime factors.


All those steps in a few lines of C++ code;
Now C++ functions come in handy, don't they?

Note: You can comment out the C++ code for the Arithmetic class from the previous lesson if you have been following.


So! C++ Fun Practice Exercise - List Prime Factors

As a fun practice exercise, feel free to try out your own different numbers, and see how the C++ code lists the prime factors of those numbers.







C++ Code for List Prime Factors - Header File.

#pragma once

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <math.h>

using namespace std;

class ListPrimeFactors {
public:
    ListPrimeFactors(unsigned);
    virtual ~ListPrimeFactors();
    string getPrimeFactors(void);
private:
    int onlyPrimeFactors(int);

    vector<unsigned> found_prime_factors;
    unsigned int find_my_factors;
    string render_factors;
    unsigned int i;
    string result;
    stringstream aux;
};


C++ Code for List Prime Factors - Class File.

#include "stdafx.h"
#include "ListPrimeFactors.h"


ListPrimeFactors::ListPrimeFactors(unsigned val) {
    found_prime_factors = {};
    find_my_factors = val;
    aux << val;
    render_factors = "The prime factors of " + aux.str() + " are: \n";
    i = 2;
}

int ListPrimeFactors::onlyPrimeFactors(int in_question) {
    int temp_limit;
    temp_limit = (int)ceil(sqrt(in_question));

    while (i <= temp_limit) {
        if (i != 1 && (in_question % i) == 0) { // avoid an infinite loop with the i != 1 check.
            found_prime_factors.push_back(i);
            return onlyPrimeFactors(in_question / i);
        }
        i++;
    }
    found_prime_factors.push_back(in_question);

    return 0;
}

/**
* Renders the prime prime factors to screen.
*/

string ListPrimeFactors::getPrimeFactors() {
    onlyPrimeFactors(find_my_factors);
    found_prime_factors.shrink_to_fit();

    //iterate through and retrieve members
    for (unsigned factor : found_prime_factors) {
        aux.str("");
        aux << factor;
        render_factors += aux.str() + " X ";
    }
    render_factors = render_factors.substr(0, render_factors.length() - 3);

    return render_factors;
}


ListPrimeFactors::~ListPrimeFactors() {
}

C++ Code for List Prime Factors - Main Class.

// Arithmetic.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ListPrimeFactors.h"

#include <iostream>

using namespace std;


int main() {
    try {

        cout << "\n    Welcome to our demonstration sequels\n";
        cout << "Hope you enjoy (and follow) the lessons.\n\n";


        unsigned int start = 1, stop = 100;

        /*
        * Prime factors of a number.
        */

        ListPrimeFactors pf_list(48);
        cout << "\n\n" << pf_list.getPrimeFactors() << "\n";

    }    catch (exception& e) {
        cout << "\n" << e.what() << "\n";
    }
    return 0;
}




<< PreviousNext >>