MY #100 DAYS OF CODE CHALLENGE JOURNEY-DAY 2

in #coding5 years ago (edited)

2.png

Problem: Finding Smallest Common Multiple in the range of two numbers e.g. 4 and 8. This implies finding the smallest common multiple in the range of 4 up to 8.

Problem Definition: smallest common multiple is a number which is the lowest number which is a multiple of the range of numbers and common to all the numbers i.e. all the range of numbers have it in common.

Algorithm

  1. I sorted out all the numbers from from highest to lowest and created a list for them.

  2. The product of the first two greatest numbers is likely to be the smallest common multiples and all the numbers in the range are being tested if they can divide this product without a remainder .

  3. If the above step return true, this will be the smallest common multiple else I test for the product multiple and divide all the range of numbers through it until I get the smallest common multiple.

  4. I then return the result.

JavaScript Code

function smallestComonMultiple(arr) {

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

let newArr = [];

for(i = arr[0]; i>=arr[1];i--){

    newArr.push(i);
}


let quot = 0;
let loop = 1;
let n;



do {
    quot = newArr[0] * loop * newArr[1] ;

    for( n = 2; n < newArr.length; n++ ){
        if(quot%newArr[n] !== 0){
            break;
        }
    }

    loop++
}

while(n!==newArr.length)

return quot;

}

console.log(smallestComonMultiple([8,4]));

Coin Marketplace

STEEM 0.27
TRX 0.11
JST 0.030
BTC 67730.39
ETH 3820.69
USDT 1.00
SBD 3.55