From: Paul Lutus Date: 2006-11-27T05:35:12+09:00 Subject: Re: coding practise sempsteen wrote: > Yes that's in my mind too. What i wrote was again a dirty, inefficient > code. I considered on the way you saw me. Here it is: > > class AmicableNumbers > def initialize(max = 1000) > @numbers = Hash.new > findFriends(max) > end > def findFriends(max) > 1.upto(max) do |j| > t1, t2 = 1, 1 > 2.upto(j / 2) {|i| t1 += i if j % i == 0} > 2.upto(t1 / 2) {|i| t2 += i if t1 % i == 0} > @numbers.store(t1, t2) if j == t2 && t1 != j > end This is very inefficient compared to creating a hash of all sums of divisors. You need to realize you are recomputing certain quantities over and over again. Maybe you would consider profiling this method and comparing it to the approach of creating a hash of all the sums. -- Paul Lutus http://www.arachnoid.com