Advertisement
Guest User

Untitled

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