Oxaric’s Blog

A compendium of amazing things…

Euler Project Problem #73 Solution

Posted by oxaric on November 24, 2008

It uses Ruby.


Click to directly download euler-solution-73.rb

# filename 'euler-solution-73.rb'
# By: Louis Casillas, oxaric@gmail.com

# Euler Problem #73
# How many fractions lie between 1/3 and 1/2 in a sorted set of reduced proper fractions?

def gcd( num1, num2 )
   if num1 < num2
      a = num2
      b = num1
   else
      a = num1
      b = num2
   end

   while true
      c = a % b

      break if c == 0
   
      a = b
      b = c      
   end
   
   b
end

bottom = 1.0 / 3.0
top = 0.5
total = 0

for d in (2..10000)
   for n in (1...d)
      temp = ((* 1.0) / d)
      if (temp > bottom) && (temp < top)
         if (gcd( d, n ) == 1)
            total += 1
         end
      end
   end

end

puts "The number of proper fractions is: " + total.to_s

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>