D
Dirk Meijer
------=_Part_8362_9999763.1138558936898
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
hi all,
i was wondering if my rubyness has increased above the 'total
newbie'-level..
so below is some code in my coding style, can you give comments on how it
looks and how effective it is.
greetings Dirk.
def find_factors( number )
factors =3D []
1.upto( number ) do | factor |
if number%factor =3D=3D 0
factors << factor
end
end
factors
end
def find_prime_numbers( max )
factor_list =3D []
1.upto( max ) do | number |
factor_list << find_factors( number )
end
factor_list
end
def output_factor_list( factor_list , detail=3Dnil )
output =3D ""
if detail
factor_list.each_with_index do | factors , number |
factors.length =3D=3D 2 ? output << "*" : output << " "
output << (number+1).to_s + ' ' *
(factor_list.length.to_s.length-(number+1).to_s.length) + "{ "
factors.each do | factor |
output << "#{factor} "
end
output << "}\n"
end
else
output << "{ "
factor_list.each_with_index do | factors , number |
output << "#{(number+1).to_s} " if factors.length =3D=3D 2
end
output << "}"
end
output
end
print output_factor_list( find_prime_numbers( ARGV[0].to_i ) , ARGV[1] )
------=_Part_8362_9999763.1138558936898--
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
hi all,
i was wondering if my rubyness has increased above the 'total
newbie'-level..
so below is some code in my coding style, can you give comments on how it
looks and how effective it is.
greetings Dirk.
def find_factors( number )
factors =3D []
1.upto( number ) do | factor |
if number%factor =3D=3D 0
factors << factor
end
end
factors
end
def find_prime_numbers( max )
factor_list =3D []
1.upto( max ) do | number |
factor_list << find_factors( number )
end
factor_list
end
def output_factor_list( factor_list , detail=3Dnil )
output =3D ""
if detail
factor_list.each_with_index do | factors , number |
factors.length =3D=3D 2 ? output << "*" : output << " "
output << (number+1).to_s + ' ' *
(factor_list.length.to_s.length-(number+1).to_s.length) + "{ "
factors.each do | factor |
output << "#{factor} "
end
output << "}\n"
end
else
output << "{ "
factor_list.each_with_index do | factors , number |
output << "#{(number+1).to_s} " if factors.length =3D=3D 2
end
output << "}"
end
output
end
print output_factor_list( find_prime_numbers( ARGV[0].to_i ) , ARGV[1] )
------=_Part_8362_9999763.1138558936898--