Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This will return the smallest commmon multiple of two numbers.
- function smallMultiple(a, b, exclude) {
- var iterate = 1;
- while (true) {
- if (a * iterate % b === 0 && a * iterate != exclude) {
- return a * iterate;
- } else {
- console.log(iterate);
- iterate++;
- }
- }
- }
- function smallestCommons(arr) {
- // 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([1,5]);
Advertisement
RAW Paste Data
Copied
Advertisement