Facebook
From Bulky Giraffe, 6 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 124
  1. function smallestCommons(arr) {
  2.   arr = arr.sort((a, b) => a > b)
  3.   let a = arr[0]
  4.   let b = arr[1]
  5.   let searchingInProgress = true
  6.   let start = 1
  7.   let next = 200
  8.   let numbers = []
  9.   let startIndex = a
  10.   for (let i = 0 ; i < (b - a + 1) ; i++) {
  11.     numbers.push([])
  12.   }
  13.  
  14.  
  15.   while (searchingInProgress) {
  16.    
  17.     let multiplies = numbers.slice(0)
  18.     //Creating 10x10 2D array of multiplies, each column represents a number in given range
  19.     multiplies = multiplies.map( (number, index) => {
  20.       let subMultiplies = []
  21.       for (let i = start ; i < next ; i++) {
  22.         subMultiplies.push((index + startIndex) * i)
  23.       }
  24.       return subMultiplies
  25.     })
  26.     //Searching through array of multiplies
  27.     multiplies = multiplies.map( (multipliesSequence, index) => {
  28.       //iterate through multiplies in sequence and returns only those which occur in every sequence
  29.        return multipliesSequence.filter( multiply => {
  30.          let alreadyExists = true
  31.          //checks if multiply exists in every column
  32.          for (let i = 0 ; i < multiplies.length ; i++) {
  33.            alreadyExists = alreadyExists &&
  34.                            multiplies[i].indexOf(multiply) !== -1
  35.          }
  36.          return alreadyExists
  37.        })
  38.     })
  39.     console.log(multiplies[0].length)
  40.      if (multiplies[0].length !== 0) console.log(multiplies[0])
  41.     if (multiplies[0].length !== 0) return multiplies[0][0]
  42.     start += next
  43.     next += next
  44.   }
  45. }
  46.  
  47.  
  48. smallestCommons([1,5]);