Arithmetic/Rational: Difference between revisions

→‎{{header|Ruby}}: Speed up by using Integer.sqrt
(added Ol)
(→‎{{header|Ruby}}: Speed up by using Integer.sqrt)
Line 3,809:
 
=={{header|Ruby}}==
Ruby has a Rational class in it's core since 1.9. Before that it was in standard library:
<lang ruby>
<lang ruby>require 'rational' #Only needed in Ruby < 1.9
 
for candidate in 2 .. 2**19
sum = Rational(1, candidate)
for factor in 2 .. Integer. sqrt(candidate**0.5)
if candidate % factor == 0
sum += Rational(1, factor) + Rational(1, candidate / factor)
1,149

edits