-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem198.rb
More file actions
executable file
·29 lines (24 loc) · 1.08 KB
/
Copy pathproblem198.rb
File metadata and controls
executable file
·29 lines (24 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# A best approximation to a real number x for the denominator bound d is a rational number r/s (in reduced form) with s d, so that any rational number p/q which is closer to x than r/s has q d.
#
# Usually the best approximation to a real number is uniquely determined for all denominator bounds. However, there are some exceptions, e.g. 9/40 has the two best approximations 1/4 and 1/5 for the denominator bound 6. We shall call a real number x ambiguous, if there is at least one denominator bound for which x possesses two best approximations. Clearly, an ambiguous number is necessarily rational.
#
# How many ambiguous numbers x = p/q, 0 x 1/100, are there whose denominator q does not exceed 108?
require 'frac'
class Integer
def closest_multiple(mul)
rem = self % mul
return ((rem < mul - rem)?(self - rem):(self + (mul-rem)))
end
end
class Frac
def best_approx(d)
#finds t/d such that t/d is closest to self
common_denominator_multiplier = lcm(d,denominator) / denominator
end
end
x=Frac.new(1,4)
a=Frac.new(2,5)
b=Frac.new(1,5)
puts b-x
puts x-a
# puts 20.closest_multiple(8)