From: Martin DeMello Date: 2003-02-06T08:49:04+09:00 Subject: Re: a cute way to reduce a fraction? Martin DeMello wrote: > 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... Knew I was overcomplicating it! den.each_index{|i| if (f = num.index(den[i])) num[f] = den[i] = nil end } num, den = num.compact, den.compact p num, den martin