#!/usr/bin/env ruby
# Solution to Project Euler problem 57
# Author: David J. Oftedal.
# A sequence of fractions approximating √2.
def sqrt2 n
numerator = 1
denominator = 1
n.times do
numerator, denominator = numerator+2*denominator, numerator+denominator
yield numerator, denominator
end
end
# Count the number of terms from 1-1000 in which the numerator is longer than the denominator.
count = 0
sqrt2(1000) { |numerator, denominator| count += 1 if numerator.to_s.length > denominator.to_s.length }
puts "The numerator is longer in " << count.to_s << " of the fractions."