kata-algorithms 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 90aac947c57fde27f904e20c09ae9276ae21f3b4
4
- data.tar.gz: 0ebcc3abc31c17bfed448377b3dab7a38019c505
3
+ metadata.gz: e796ad4d1ba7ced44cbcb08c5d8e76609d0ebec8
4
+ data.tar.gz: 3450d795d0db2cc5d0a80256b75c9bbe987c68b3
5
5
  SHA512:
6
- metadata.gz: e8ce09b15a615dc8c1f1af17fd194e760f48cc7ff4f61a7a5aac09ef828a97d5ec8cc692d00cf97c592ea3b5b6a6626c41a0caf8e6471020283214afa34f436c
7
- data.tar.gz: 26624f7cfee3cec901b680c3477d52d5716beae2df75b97c1fda9cbbca2ec14a2ece0f53778b887fb5157edb07914249a9fd6e058fce3fba324c9c73135d3c0c
6
+ metadata.gz: cc7265967142ef4aea31e8375e394a048161bd497d03c4be830b61fff9305eb3fca31e31779d87d35292f6412be073f06a03c934c705e337a73ca584a6b0b366
7
+ data.tar.gz: 4972a622b67c9e11a9a6a9e4afead3e80e4522ce04eb1e8cbd57c5a7823b029976c75d593fb5c2dbcfc3cf6f8fedba1c0333b8c8548f2c27d31772082d704efe
data/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+ v0.2.0 Added algorithms for calculating GCD
data/bin/gcd_modulo ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "kata/algorithms"
4
+
5
+ print "Give me the first number: "
6
+ a = gets.strip.to_i
7
+
8
+ print "Give me the second number: "
9
+ b = gets.strip.to_i
10
+
11
+ result = Kata::Algorithms.gcd_recursive_modulo(a, b)
12
+
13
+ puts "Result is #{result}"
14
+
15
+
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "kata/algorithms"
4
+
5
+ print "Give me the first number: "
6
+ a = gets.strip.to_i
7
+
8
+ print "Give me the second number: "
9
+ b = gets.strip.to_i
10
+
11
+ result = Kata::Algorithms.gcd_recursive_modulo(a, b)
12
+
13
+ puts "Result is #{result}"
14
+
15
+
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "kata/algorithms"
4
+
5
+ print "Give me the first number: "
6
+ a = gets.strip.to_i
7
+
8
+ print "Give me the second number: "
9
+ b = gets.strip.to_i
10
+
11
+ result = Kata::Algorithms.gcd_recursive_subtraction(a, b)
12
+
13
+ puts "Result is #{result}"
14
+
15
+
@@ -1,5 +1,5 @@
1
1
  module Kata
2
2
  module Algorithms
3
- VERSION = "0.0.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -130,5 +130,84 @@ module Kata
130
130
  end
131
131
  result
132
132
  end
133
+
134
+ # GREATEST COMMON DIVISOR (recursive implementation with modulo)
135
+ # ======================= --------------------------------------
136
+ #
137
+ # We are using the Euclidean algorithm to calculate the GCD or Greatest Common Divisor.
138
+ # This is also called GCF (Greatest Common Factor) or HCF (Highest Common Factor).
139
+ #
140
+ # The algorithm goes as follows:
141
+ #
142
+ # gcd(a, 0) is a
143
+ # gcd(a, b) is equal to gcd(b, a mod b) - consider a >= b
144
+ #
145
+ # We are going to use a recursive call to implement it.
146
+ #
147
+ def self.gcd_recursive_modulo(a, b)
148
+ return b if a == 0
149
+ return a if b == 0
150
+ if a >= b
151
+ gcd_recursive_modulo(b, a % b)
152
+ else
153
+ gcd_recursive_modulo(a, b % a)
154
+ end
155
+ end
156
+
157
+ # GREATEST COMMON DIVISOR (recursive implementation with subtraction)
158
+ # ======================= -------------------------------------------
159
+ #
160
+ # We are using the Euclidean algorithm to calculate the GCD or Greatest Common Divisor.
161
+ # This is also called GCF (Greatest Common Factor) or HCF (Highest Common Factor).
162
+ #
163
+ # The algorithm goes as follows:
164
+ #
165
+ # gcd(a, 0) is a
166
+ # gcd(a, b) is equal to gcd(a - b, b) - consider a >= b
167
+ #
168
+ # We are going to use a recursive call to implement it.
169
+ #
170
+ def self.gcd_recursive_subtraction(a, b)
171
+ return b if a == 0
172
+ return a if b == 0
173
+ if a >= b
174
+ gcd_recursive_subtraction(a - b, b)
175
+ else
176
+ gcd_recursive_subtraction(b - a, a)
177
+ end
178
+ end
179
+
180
+ # GREATEST COMMON DIVISOR (implementation with modulo)
181
+ # ======================= ----------------------------
182
+ #
183
+ # We are using the Euclidean algorithm to calculate the GCD or Greatest Common Divisor.
184
+ # This is also called GCF (Greatest Common Factor) or HCF (Highest Common Factor).
185
+ #
186
+ # The algorithm goes as follows:
187
+ #
188
+ # a |- b => quotient and remainder
189
+ # if remainder == 0 then b is the GCD
190
+ # else a = b, b = r
191
+ # and repeat
192
+ #
193
+ def self.gcd_modulo(a, b)
194
+ return b if a == 0
195
+ return a if b == 0
196
+
197
+ if b > a
198
+ temp = b
199
+ b = a
200
+ a = temp
201
+ end
202
+ # now a >= b
203
+
204
+ r = a % b
205
+ while r > 0
206
+ a = b
207
+ b = r
208
+ r = a % b
209
+ end
210
+ b
211
+ end
133
212
  end
134
213
  end
@@ -97,5 +97,131 @@ describe Kata::Algorithms do
97
97
  it { expect(subject.multiplication(a, b)).to eq((a.to_i * b.to_i).to_s) }
98
98
  end
99
99
  end
100
+ describe '.gcd_recursive_modulo' do
101
+ context 'when a is 48 and b is 18' do
102
+ let(:a) { 48 }
103
+ let(:b) { 18 }
104
+ it { expect(subject.gcd_recursive_modulo(a, b)).to eq(6) }
105
+ end
106
+ context 'when a is 100 and b is 50' do
107
+ let(:a) { 100 }
108
+ let(:b) { 50 }
109
+ it { expect(subject.gcd_recursive_modulo(a, b)).to eq(50) }
110
+ end
111
+ context 'when a is 10000 and b is 1' do
112
+ let(:a) { 1000 }
113
+ let(:b) { 1 }
114
+ it { expect(subject.gcd_recursive_modulo(a, b)).to eq(1) }
115
+ end
116
+ context 'when a is 8 and b is 12' do
117
+ let(:a) { 8 }
118
+ let(:b) { 12 }
119
+ it { expect(subject.gcd_recursive_modulo(a, b)).to eq(4) }
120
+ end
121
+ context 'when a is 54 and b is 24' do
122
+ let(:a) { 54 }
123
+ let(:b) { 24 }
124
+ it { expect(subject.gcd_recursive_modulo(a, b)).to eq(6) }
125
+ end
126
+ context 'when a is 18 and b is 84' do
127
+ let(:a) { 18 }
128
+ let(:b) { 84 }
129
+ it { expect(subject.gcd_recursive_modulo(a, b)).to eq(6) }
130
+ end
131
+ context 'when a is 48 and b is 180' do
132
+ let(:a) { 48 }
133
+ let(:b) { 180 }
134
+ it { expect(subject.gcd_recursive_modulo(a, b)).to eq(12) }
135
+ end
136
+ context 'when a is 1 and b is 1' do
137
+ let(:a) { 1 }
138
+ let(:b) { 1 }
139
+ it { expect(subject.gcd_recursive_modulo(a, b)).to eq(1) }
140
+ end
141
+ end
142
+ describe '.gcd_recursive_subtraction' do
143
+ context 'when a is 48 and b is 18' do
144
+ let(:a) { 48 }
145
+ let(:b) { 18 }
146
+ it { expect(subject.gcd_recursive_subtraction(a, b)).to eq(6) }
147
+ end
148
+ context 'when a is 100 and b is 50' do
149
+ let(:a) { 100 }
150
+ let(:b) { 50 }
151
+ it { expect(subject.gcd_recursive_subtraction(a, b)).to eq(50) }
152
+ end
153
+ context 'when a is 10000 and b is 1' do
154
+ let(:a) { 1000 }
155
+ let(:b) { 1 }
156
+ it { expect(subject.gcd_recursive_subtraction(a, b)).to eq(1) }
157
+ end
158
+ context 'when a is 8 and b is 12' do
159
+ let(:a) { 8 }
160
+ let(:b) { 12 }
161
+ it { expect(subject.gcd_recursive_subtraction(a, b)).to eq(4) }
162
+ end
163
+ context 'when a is 54 and b is 24' do
164
+ let(:a) { 54 }
165
+ let(:b) { 24 }
166
+ it { expect(subject.gcd_recursive_subtraction(a, b)).to eq(6) }
167
+ end
168
+ context 'when a is 18 and b is 84' do
169
+ let(:a) { 18 }
170
+ let(:b) { 84 }
171
+ it { expect(subject.gcd_recursive_subtraction(a, b)).to eq(6) }
172
+ end
173
+ context 'when a is 48 and b is 180' do
174
+ let(:a) { 48 }
175
+ let(:b) { 180 }
176
+ it { expect(subject.gcd_recursive_subtraction(a, b)).to eq(12) }
177
+ end
178
+ context 'when a is 1 and b is 1' do
179
+ let(:a) { 1 }
180
+ let(:b) { 1 }
181
+ it { expect(subject.gcd_recursive_subtraction(a, b)).to eq(1) }
182
+ end
183
+ end
184
+ describe '.gcd_modulo' do
185
+ context 'when a is 48 and b is 18' do
186
+ let(:a) { 48 }
187
+ let(:b) { 18 }
188
+ it { expect(subject.gcd_modulo(a, b)).to eq(6) }
189
+ end
190
+ context 'when a is 100 and b is 50' do
191
+ let(:a) { 100 }
192
+ let(:b) { 50 }
193
+ it { expect(subject.gcd_modulo(a, b)).to eq(50) }
194
+ end
195
+ context 'when a is 10000 and b is 1' do
196
+ let(:a) { 1000 }
197
+ let(:b) { 1 }
198
+ it { expect(subject.gcd_modulo(a, b)).to eq(1) }
199
+ end
200
+ context 'when a is 8 and b is 12' do
201
+ let(:a) { 8 }
202
+ let(:b) { 12 }
203
+ it { expect(subject.gcd_modulo(a, b)).to eq(4) }
204
+ end
205
+ context 'when a is 54 and b is 24' do
206
+ let(:a) { 54 }
207
+ let(:b) { 24 }
208
+ it { expect(subject.gcd_modulo(a, b)).to eq(6) }
209
+ end
210
+ context 'when a is 18 and b is 84' do
211
+ let(:a) { 18 }
212
+ let(:b) { 84 }
213
+ it { expect(subject.gcd_modulo(a, b)).to eq(6) }
214
+ end
215
+ context 'when a is 48 and b is 180' do
216
+ let(:a) { 48 }
217
+ let(:b) { 180 }
218
+ it { expect(subject.gcd_modulo(a, b)).to eq(12) }
219
+ end
220
+ context 'when a is 1 and b is 1' do
221
+ let(:a) { 1 }
222
+ let(:b) { 1 }
223
+ it { expect(subject.gcd_modulo(a, b)).to eq(1) }
224
+ end
225
+ end
100
226
  end
101
227
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kata-algorithms
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Panayotis Matsinopoulos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-29 00:00:00.000000000 Z
11
+ date: 2013-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -43,17 +43,24 @@ email:
43
43
  - panayotis@matsinopoulos.gr
44
44
  executables:
45
45
  - addition
46
+ - gcd_modulo
47
+ - gcd_recursive_modulo
48
+ - gcd_recursive_subtraction
46
49
  - multiplication
47
50
  extensions: []
48
51
  extra_rdoc_files: []
49
52
  files:
50
53
  - .gitignore
51
54
  - .rvmrc
55
+ - CHANGELOG
52
56
  - Gemfile
53
57
  - LICENSE.txt
54
58
  - README.md
55
59
  - Rakefile
56
60
  - bin/addition
61
+ - bin/gcd_modulo
62
+ - bin/gcd_recursive_modulo
63
+ - bin/gcd_recursive_subtraction
57
64
  - bin/multiplication
58
65
  - kata-algorithms.gemspec
59
66
  - lib/kata/algorithms.rb