Jules Jacobs wrote:
> Hi Dirk,
> [...lot's of stuff i also did :) ...]
> The output method is still very tricky, and not elegant. I first changed
> factor_list.length.to_s.length to Math.log10(list.length).ceil. Then I
> moved it out of the loop, because it is more efficient (it will only be
> computed once, and not again in every iteration). I think you know what
> log10 is? Well, log10 and then ceil (round up) returns the number of
> numbers in a number. So 23 has 2 numbers, and 1234 has 4.

--------------------------------------------------------------
def find_factors(number)
(1..number).select{|factor| (number % factor).zero?}
end

def find_prime_numbers(max)
(1..max).map{|number| find_factors(number)}
end

def output_factor_list( factor_list , detail=nil )
if detail
(1..factor_list.size).map do |number|
(factor_list[number - 1].length == 2 ? '*' : ' ') +
number.to_s.ljust(factor_list.length.to_s.length) +
'{ ' + factor_list[number - 1].join(' ') + '}'
end.join("\n")
else
"{ "+factor_list.select{|factors| factors.length == 2}.join(' ')+'}'
end
end

puts output_factor_list( find_prime_numbers( 20 ) , true)
--------------------------------------------------------------

> I think most of the changes are from imperative to functional.
> Inject/map/select /join really make things simpler, but they are hard to
> understand if you haven't used them.

It's not that hard, and you will feel like someone cut of your right
hand if you have to live without them again.

cheers

Simon