From: Martin DeMello Date: 2003-02-06T07:28:44+09:00 Subject: Re: a cute way to reduce a fraction? Chris Pine wrote: > Hello, > > I'm trying to find a pretty way to reduce a fraction. Assuming I have two > arrays, `num' and `den', containing the prime factors of the numerator and > the denomenator, I'd like some nice way to remove identical numbers and thus > reduce the fraction. So it would look like this: > > num = [2, 2, 3] # 12 > den = [2, 3] # 6 > > # ...your pretty ruby here... # Okay, not all that pretty, but def enc(a) h = Hash.new(0) a.map {|n| "#{n}.#{h[n]+=1}"} end def diff(a,b) (enc(a) - enc(b)).map {|i| i.to_i} end num, den = diff(num, den), diff(den, num) > num # --> [2] > den # --> [] You're right, there should be a simpler way! martin