-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem33.rb
More file actions
47 lines (40 loc) · 1.21 KB
/
problem33.rb
File metadata and controls
47 lines (40 loc) · 1.21 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require("./utils")
class Problem33
def Problem33.run
numerator = 1
denominator = 1
(10..99).each do |x|
(10..99).each do |y|
next if x % 10 == 0 and y % 10 == 0
value = Float(x) / Float(y)
next if value >= 1
a = Float(String(x)[0..0])
b = Float(String(x)[1..1])
c = Float(String(y)[0..0])
d = Float(String(y)[1..1])
good = false
if b == d and a / c == value
good = true
puts "#{x}/#{y} = #{Integer(a)}/#{Integer(c)}"
elsif a == d and y % 10 != 0 and b / c == value
good = true
puts "#{x}/#{y} = #{Integer(b)}/#{Integer(c)}"
elsif b == c and y % 10 != 0 and a / d == value
good = true
puts "#{x}/#{y} = #{Integer(a)}/#{Integer(d)}"
elsif a == c and b / d == value
good = true
puts "#{x}/#{y} = #{Integer(b)}/#{Integer(d)}"
end
if good
numerator *= x
denominator *= y
end
end
end
num_factors = Utils.get_factors numerator
den_factors = Utils.get_factors denominator
gcd = (num_factors & den_factors).max
puts "Answer: #{numerator / gcd}/#{denominator / gcd}"
end
end