Sha256: b49d6fbcadd846ec6c32cff184e2cbe2a09cab0c59e0583ae2c4f43c47c83e08

Contents?: true

Size: 1.44 KB

Versions: 396

Compression:

Stored size: 1.44 KB

Contents

class Palindromes
  constructor: (bounds) ->
    @max = bounds['maxFactor']
    @min = bounds['minFactor'] || 1
    @range = [@min..@max]
    @palindromes = @generate()

  generate: ->
    palindromes = []
    for i in [@min..@max]
      for j in [@min..@max]
        product = (i * j)
        palindromes.push(product) if @isPalindrome(product)
    return palindromes

  uniqueFactors: (value) =>
    starting = @findFactors(value)
    sorted = starting.map (pair) -> pair.sort()
    return sorted.unique()

  findFactors: (value) ->
    results = []
    for i in [1..value]
      results.push([i, (value / i)]) if value % i == 0
    return results

  largest: =>
    biggest = Math.max.apply(Math, @palindromes)
    low = @min
    high = @max
    return {
      value: biggest,
      factors: @uniqueFactors(biggest).filter (pair) ->
        pair.every (factor) -> low <= factor <= high
    }

  smallest: =>
    littlest = Math.min.apply(Math, @palindromes)
    low = @min
    high = @max
    return {
      value: littlest,
      factors: @uniqueFactors(littlest).filter (pair) ->
        pair.every (factor) -> low <= factor <= high
    }

  isPalindrome: (number) ->
    return `"".split.call(number, "").reverse().join("") == number`

Array::unique = ->
  output = {}
  output[@[key]] = @[key] for key in [0...@length]
  value for key, value of output

arrayEqual = (a, b) ->
  a.length is b.length and a.every (elem, i) -> elem is b[i]

module.exports = Palindromes

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.179 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.178 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.177 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.176 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.175 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.174 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.173 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.172 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.171 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.170 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.169 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.167 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.166 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.165 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.164 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.163 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.162 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.161 tracks/coffeescript/exercises/palindrome-products/example.coffee
trackler-2.2.1.160 tracks/coffeescript/exercises/palindrome-products/example.coffee