Factors of an integer: Difference between revisions

→‎{{header|Ruby}}: improvement of the output
(→‎{{header|Ruby}}: improvement of the output)
Line 2,563:
As we only have to loop up to <math>\sqrt{n}</math>, we can write
<lang ruby>class Integer
def factors()
1.upto(Math.sqrt(self)).select {|i| (self % i).zero?}.inject([]) do |f, i|
f << i
f << self/i unless i == self/i
f << i
end.sort
end
end
[45, 53, 64].each {|n| pputs "#{n} : #{n.factors}"}</lang>
{{out}}
output
<pre>
<pre>45 : [1, 3, 5, 9, 15, 45]
53 : [1, 53]
64 : [1, 2, 4, 8, 16, 32, 64]</pre>
 
 
=={{header|Run BASIC}}==
Anonymous user