From: "Benjamin J. Tilly" Date: 2001-03-10T15:21:58+09:00 Subject: [ruby-talk:12375] Re: ...and the challenge This version is the fastest on my machine. YMMV. Where it differs from my previous one is that I avoid redoing the multiplications by the generators. I tried a few other designs out, but this seemed best. Incidentally it is mildly irritating, but the to_i method does not strip out _. So this will not do what you want if you do: ruby hamming.rby 1_000_000 You need to make that ruby hamming.rby 1000000 Cheers, Ben class HammingSeq attr_reader :generators attr :pos attr :seq attr :vals def initialize (*generators) if 0 == generators.length raise "Cannot create a sequence without generators" end @generators = generators @pos = Array.new (generators.length, 0) @vals = generators.dup # Need elements 0 and 1 to avoid infinite recursion @seq = [1, @generators.min] # We have the first elem manually inserted, so throw one away. next_elem end def [] (n) while @seq.length <= n @seq.push next_elem end @seq[n] end def next_elem min_val = @vals.min @vals.each_index {|i| if min_val == vals[i] @pos[i] += 1 @vals[i] = @generators[i] * @seq[@pos[i]] end } min_val end end hamming = HammingSeq.new(2, 3, 5) max = ARGV[0].to_i for i in 0..max do val = hamming[i] if val <= max puts val else break end end puts "Done" ------------------------------------------- The Fastest Browser on Earth now for FREE!! Download Opera 5 for Windows now! Get it at http://www.opera.com/download/ -------------------------------------------