-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcd.rb
55 lines (49 loc) · 948 Bytes
/
gcd.rb
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
48
49
50
51
52
53
54
55
# 02B: Greatest Common Divisor
# requires the `rubylabs` gem for the Source module
# brute force algorithm
#
# :begin :gcd1
def gcd1(a, b)
t = [a, b].min
t.downto(1) do |t|
t # just evaluate the expression so that RubyLabs#count works properly
# as the next statement isn't counted otherwise
return t if a % t == 0 && b % t == 0
end
end
# :end :gcd1
# dijkstra's algorithm
#
# :begin :gcd2
def gcd2(a, b)
until a == b
if a > b
a = a - b
else
b = b - a
end
end
a
end
# :end :gcd2
# euclid's algorithm
#
# :begin :gcd3
def gcd3(a, b)
until b == 0
t = b
b = a % b
a = t
end
a
end
# :end :gcd3
Source.clear
Source.probe("gcd1", 5, :count)
Source.probe("gcd2", 3, :count)
Source.probe("gcd3", 3, :count)
def run(a, b)
[:gcd1, :gcd2, :gcd3].each do |fn|
puts "#{fn} runs for #{count { self.send(fn, a, b) }} times, taking #{time { self.send(fn, a, b) }.to_s} sec"
end
end