Advertisement
Guest User

Untitled

a guest
May 4th, 2017
191
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This will return the smallest commmon multiple of two numbers.
  2. function smallMultiple(a, b, exclude) {
  3.     var iterate = 1;
  4.     while (true) {
  5.         if (a * iterate % b === 0 && a * iterate != exclude) {
  6.             return a * iterate;
  7.         } else {
  8.             console.log(iterate);
  9.             iterate++;
  10.         }
  11.     }
  12. }
  13.  
  14. function smallestCommons(arr) {
  15.     // creating an array containing all the numbers that need to be checked against.
  16.     var range = [];
  17.     for (var i = 1; i < arr[1] + 1; i++) {
  18.         range.push(i);
  19.     }
  20.    
  21.     // sorting array in descending order.
  22.     range.sort(function(a,b) {
  23.         return b - a;
  24.     });
  25.    
  26.     // finding a multiple of the first two numbers and checking it against
  27.     // the next numnbers. Continuing until a multiple is found that matches
  28.     // all of the numbers in range.
  29.     var noCheck = 0;
  30.     while (true) {
  31.         var check = smallMultiple(range[0], range[1], noCheck);
  32.        
  33.         for (var n = 2; n < range.length; n ++) {
  34.             if (check % range[n] === 0 && n == range.length -1) {
  35.                 console.log(check);
  36.                 return check;
  37.             } else if (check % range[n] !== 0) {
  38.                 noCheck = check;
  39.                 break;
  40.             }
  41.         }
  42.     }
  43. }
  44.  
  45.  
  46. smallestCommons([1,5]);
Advertisement
RAW Paste Data Copied
Advertisement