From: sempsteen Date: 2006-11-27T05:04:08+09:00 Subject: Re: coding practise 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 end def has_friend?(number) return @numbers.has_key?(number) end def friend_of(number) return @numbers[number] end end amicable_numbers = AmicableNumbers.new(5000) 1.upto(5000) {|i| print i, "\t<->\t", amicable_numbers.friend(i), "\n" if amicable_numbers.has_friend?(i)} I know there are a lot of improvement points here but they are not so important for me right now. I can improve later after constructing the true structure. In above example i have a class definition of amicable numbers. When i call its constructer method with no arguments it finds the amicable numbers within the range of 0..1000 and stores them at @numbers instance variable. By "has_friend?" and "friend_of" methods i'm looking for a friend number and getting the value of the friend number for a given number. I'll improve it or maybe write a new one and post it here as i gain more info and experience on Ruby. On 11/26/06, Paul Lutus wrote: > sempsteen wrote: > > > i did it, thanks again! > > Usenet is a door that swings both ways. This thread may last decades in > Usenet archives and may be accessed again and again by students and others. > > What am I saying? I am saying you might consider posting your final code, > for the benefit of all who might try to solve the same problem in the > future. > > -- > Paul Lutus > http://www.arachnoid.com > >