Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def run(n)
- result = findRuns(getMarchingWidthValues(n).reverse)
- puts "#{result.inspect}"
- end
- def getMarchingWidthValues(n)
- Array.new(n) {|i| [i + 2, biggestFactorSmallerThanSquareRoot(i + 2)]}
- end
- def biggestFactorSmallerThanSquareRoot(n)
- arr = divisors(n).select{|x| x <= squareRootFloor(n)}
- val = arr.pop
- if val ** 2 == n
- val = arr.pop
- end
- return val
- end
- def findRuns(arr)
- runs = Hash.new
- target = 1
- currentRun = []
- arr.each_index do |i|
- if target == arr[i][1]
- currentRun.push(arr[i])
- target = target + 1
- else
- runs[currentRun.length] = currentRun
- currentRun = []
- target = 1
- end
- end
- return runs
- end
- def squareRootFloor(n)
- min = 1
- max = n / 2
- while (max - min > 1)
- g = (max + min) / 2
- if g ** 2 == n
- return g
- end
- if g ** 2 > n
- max = g
- else
- min = g
- end
- end
- return min
- end
- def divisors(n)
- result = []
- max = squareRootFloor(n) + 1
- k = 1
- while k <= max
- if n % k == 0
- result.push(k)
- result.push(n / k)
- end
- k = k + 1
- end
- return result.uniq.sort
- end
- run 1000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement