module Multiples::MultiplesEnumGenerator

Public Class Methods

new(a, b) click to toggle source
# File lib/multiples/multiples_enumerator.rb, line 4
def new a, b
  @a, @b = (a..Float::INFINITY).step(a), (b..Float::INFINITY).step(b)
  @palindrome_length = a + b - 1
  @stack = [[@a.peek, @b.peek].min.to_i]
  send :build_palindrome
  return MultiplesEnumerator.new(@stack)
end

Private Class Methods

build_palindrome() click to toggle source
# File lib/multiples/multiples_enumerator.rb, line 13
def build_palindrome
  until @stack.length == @palindrome_length
    value = step

    # Palindrome completed! Even numbers used!
    break if value.zero? 

    @stack << value
  end
end
step() click to toggle source
# File lib/multiples/multiples_enumerator.rb, line 24
def step
  val = [@a.peek, @b.peek].max - [@a.peek, @b.peek].min
  if val > @stack[0]
    val = @stack[0]
  end
  h = {@a.peek => @a, @b.peek => @b}
  h[h.keys.min].next
  val.to_i
end