From: Mathieu Bouchard Date: 2001-03-10T03:08:24+09:00 Subject: [ruby-talk:12344] Re: ...and the challenge > On Sat, 10 Mar 2001, Robert Feldt wrote: > > Hi again, > > Yes I forgot the challenge: > > Write a nice, fast and small Ruby program for generating the n > > first numbers in the Hamming sequence defined as > > "List of all numbers, in ascending order, of the form > > (2**i)*(3**j)*(5**k) for i,j,k at least 0. It begins like > > 1 2 3 4 5 6 8 9 10 12 15 16 18 ..." > Here's one for a start. it's not fast though. > def suck(i,x); while x%i==0; x/=i; end; x; end > n=1; loop do p n if 1 == (suck 2, suck 3, suck 5, n); n+=1; end limiting it to 1_000_000, it takes 27 seconds to run. I then tried this one: n=1_000_000 nums=[] (0..20).each{|a| (0..13).each{|b| (0..9).each{|c| x = 2**a * 3**b * 5**c nums << x if x <= n } } } nums.sort! nums.each{|x|p x} This takes 0.35 second here. matju