What is LCM? | Maths Explanation for C++ Kids
Akin to finding H.C.F., L.C.M. is commonly found by repeated factorization.
Only this time, the factors do not have to be common amongst the set of numbers.
If we have the set of numbers 8, 12 and
18 for example, their L.C.M. is found thus:
Hence, L.C.M. of 8, 12 and 18 = 2 X 2 X 2 x 3 x 3 = 72
Step-by-Step Guide to L.C.M. by Factorisation in C++
We shall follow the steps below in writing our C++ LCM code.
Step 1:
Do a numerical reverse sort on the (resulting) set so its first member is the largest in the set.
Step 2:
Starting with 2, iteratively check through the set of numbers for individual factors.
Step 3:
For each individual factor, divide affected member(s) of the number set by the factor.
Step 4:
Repeat the above steps recursively until there are no more individual factors.
Create a new C++ class file;
Call it LCM.
Type out the adjoining C++ code for finding Lowest Common Multiple (L.C.M.).
Note: You can comment out the C++ code for the main class from the previous lesson if you have been following.
So! C++ Fun Practice Exercise - Find LCM
As a fun practice exercise, feel free to try out your own numbers, and see how the C++ code finds the LCM of those numbers.
C++ Code for LCM - Header File.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class LCM {
public:
LCM(vector<unsigned>);
virtual ~LCM();
unsigned getLCM(void);
private:
int LCMFactors(void);
unsigned * set_of_numbers;
unsigned * arg_copy; // Java passes arrays by reference; make a copy.
size_t array_length;
vector<unsigned> all_factors; // factors common to our set_of_numbers
unsigned int index; // index into array common_factors
bool state_check; // variable to keep state
unsigned int calc_result;
};
C++ Code for LCM - Class File.
#include "LCM.h"
// descending sort function
bool isGreater(int i, int j) { return i > j; }
LCM::LCM(vector<unsigned> group) {
all_factors = {};
group.shrink_to_fit();
sort(group.begin(), group.end());
array_length = group.size();
set_of_numbers = new unsigned[array_length];
arg_copy = new unsigned[array_length];
index = 0;
//iterate through and retrieve members
for (unsigned number : group) {
set_of_numbers[index] = number;
index++;
}
// Sort array in descending order
sort(set_of_numbers, set_of_numbers + array_length, isGreater);
index = 2;
state_check = false;
}
/**
* Our function checks 'set_of_numbers'; If it finds a factor common to all
* for it, it records this factor; then divides 'set_of_numbers' by the
* common factor found and makes this the new 'set_of_numbers'. It continues
* recursively until all common factors are found.
*
*/
int LCM::LCMFactors() {
// Copy 'set_of_numbers' into 'arg_copy'
for (unsigned count=0; count < array_length; count++ ) {
arg_copy[count] = set_of_numbers[count];
}
// sort in descending order
sort(arg_copy, arg_copy + array_length, isGreater);
while (index <= arg_copy[0]) {
state_check = false;
for (unsigned j = 0; j < array_length; j++) {
if ((set_of_numbers[j] % index) == 0) {
set_of_numbers[j] /= index;
if (state_check == false) {
all_factors.push_back(index);
}
state_check = true;
}
}
if (state_check == true) {
return LCMFactors();
}
index++;
}
return 0;
}
/**
* Just calls out and collects the prepared factors.
*
* @param - None.
*/
unsigned LCM::getLCM() {
index = 2;
LCMFactors();
all_factors.shrink_to_fit();
//iterate through and retrieve members
calc_result = 1;
for (unsigned factor : all_factors) {
calc_result *= factor;
}
return calc_result;
}
LCM::~LCM() {
delete[] set_of_numbers;
delete[] arg_copy;
}
C++ Code for LCM - Main Class.
//
#include "stdafx.h"
#include "LCM.h"
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main() {
try {
cout << "\n Welcome to our demonstration sequels\n";
cout << "Hope you enjoy (and follow) the lessons.\n\n";
vector<unsigned> set;
/*
* Find L.C.M
*/
set = {2, 5, 7};
stringstream aux;
string render_result = "The LCM of ";
for (unsigned number : set) {
aux.str("");
aux << number;
render_result += aux.str() + "; ";
}
render_result += " is: ";
LCM lcm(set);
cout << "\n\n" << render_result << lcm.getLCM() << "\n";
} catch (exception& e) {
cout << "\n" << e.what() << "\n";
}
return 0;
}