 # Any thoughts on my Smallest Common Multiple solution using Table Method?

I initially tried using prime factorization. I spent quite a while banging my sleep deprived head against the wall of bad recursion methods before calling it a night. Today I wanted to try a different method instead (after I figured this out , I went back and tried the Euclid Method too (more straightforward) but that’s not relevant).

This is dirty, but how dirty I’m curious. Has anyone else tried the Table Method? I keep seeing Euclid or Prime Factorization so far.

I got so deep in this, I don’t really have any self-critical perspective. Just nice to have finally broken through.

``````function smallestCommons(arr) {
arr = arrayGenerate(arr);

var primes =  [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
var primesIndex = 0;
var currentPrime = 2;

var factorsArr = [];
var tempPrime = false;

while (primesIndex < primes.length - 1){
currentPrime = primes[primesIndex];

//if any of the numbers can be evenly divided, replace its original value with the result
arr = arr.map(function(val){
if (val % currentPrime === 0){
tempPrime = true;
return val/currentPrime;
} else {
return val;
}
});

//check if any of the numbers were able to be evenly divided and save the factor
//ALSO don't increment prime divisor, repeat with same instead
if (tempPrime === true){
factorsArr.push(currentPrime);
tempPrime = false;
continue;
}

//check if all numbers have been reduced to 1 (lowest factor) which means all factors found
if (arr.reduce(function(a,b){
return a * b;
}) === 1){
break;
} else {

primesIndex++;
}

}

return factorsArr.reduce(function(a,b){
return a*b;
});
}

//create array of numbers between two args (1 removed for being unnecessary)
function arrayGenerate(arr){
var newArr = [];

arr.sort(function(a,b){
return a-b;
});

if (arr === 1){
arr = 2;
}

for (var i = arr; i <= arr; i++){
newArr.push(i);
}

return newArr;
}

smallestCommons([1, 5]);

``````

Nice~~~

Someone wrote a somehow related procedure similar to mine.

Yo don’t have to put the prime numbers by yourself, as you can generate it. Also, it is probable that these are contained in the argument.

``````function smallestCommons(arr): number {
// Ordering array
arr.sort((a, b) => {
return a - b;
});
// Making array
let dividers = Array.from({ length: arr + 1 }, (v, k) => k);
dividers = dividers.slice(dividers.indexOf(arr));

// Preparing prime array
let prePrime = Array.from({ length: arr + 1 }, (v, k) => k);

if (prePrime.indexOf(1) != -1) {
prePrime = prePrime.slice(prePrime.indexOf(1) + 1);
}
let div = prePrime.filter(n => {
let m = n - 1;
while (m > 1 && m >= Math.sqrt(n)) {
if (n % m === 0) return false;
m--;
}
return true;
});

// Comparing array to get LCM

let accum = 1;
for (let j = 0; j < div.length; j++) {
// moving through elements in prime array
while (dividers.some(elem => elem % div[j] == 0)) {
for (let i = 0; i <= dividers.length; i++) {
// moving through elements in argument array
if (dividers[i] % div[j] == 0) {
// changing values in argument array
dividers[i] = dividers[i] / div[j];
}
if (i == dividers.length - 1) {
accum = accum * div[j];

}
}
}
}

return accum;
}
``````

Tested it and it worked. Kind of troublesome and not refined. But feeling happy that works as intended, now have to look how to calculate if its efficient.

Thanks for the effort, your procedure will help me improve mine.