Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- // This will return the smallest commmon multiple of two numbers.
- function smallMultiple(a, b, iterate) {
- if (iterate === 0) {
- iterate = 1;
- } else {
- iterate = (iterate / a) + 1;
- }
- while (a * iterate % b !== 0) {
- console.log(iterate);
- iterate++;
- }
- return a * iterate;
- }
- function smallestCommons(arr) {
- // making sure array is arranged from smallest to largest.
- arr = arr.sort(function(a,b) {
- return a - b;
- });
- // creating an array containing all the numbers that need to be checked against.
- var range = [];
- for (var i = 1; i < arr[1] + 1; i++) {
- range.push(i);
- }
- // sorting array in descending order.
- range.sort(function(a,b) {
- return b - a;
- });
- // finding a multiple of the first two numbers and checking it against
- // the next numnbers. Continuing until a multiple is found that matches
- // all of the numbers in range.
- var noCheck = 0;
- while (true) {
- var check = smallMultiple(range[0], range[1], noCheck);
- for (var n = 2; n < range.length; n ++) {
- if (check % range[n] === 0 && n == range.length -1) {
- console.log(check);
- return check;
- } else if (check % range[n] !== 0) {
- noCheck = check;
- break;
- }
- }
- }
- }
- smallestCommons([18,23]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement