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