briandoll-greatest_common_factor 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -1
- data/README.txt +4 -0
- data/greatest_common_factor.gemspec +1 -1
- data/lib/greatest_common_factor.rb +11 -10
- metadata +1 -1
data/History.txt
CHANGED
@@ -1,3 +1,9 @@
|
|
1
1
|
== 0.0.1 / 2008-09-19
|
2
|
-
|
3
2
|
* Initial release
|
3
|
+
|
4
|
+
== 0.0.2 / 2008-10.10
|
5
|
+
* Added speed improvements when dealing with arrays containing a large
|
6
|
+
spread of numbers
|
7
|
+
* Added benchmarking code to compare 2-digit array calculation with
|
8
|
+
basic ruby
|
9
|
+
* Added fuzzy gcf calculations
|
data/README.txt
CHANGED
@@ -10,11 +10,15 @@ This gem provides that same ability on arrays of integers.
|
|
10
10
|
== FEATURES:
|
11
11
|
* Calculate the GCF on an array of integers
|
12
12
|
* Factor the array of integers by the GCF
|
13
|
+
* Calculate non-exact GCF with a specified tolerance
|
13
14
|
|
14
15
|
== SYNOPSIS:
|
15
16
|
* [12,16,8,40].greatest_common_factor => 4
|
16
17
|
* [12,16,8,40].gcf => 4
|
17
18
|
* [12,16,8,40].factored_by_gcf => [3, 4, 2, 10]
|
19
|
+
* Fuzzy:
|
20
|
+
* [28,38,73,93].factored_by_gcf => nil
|
21
|
+
* [28,38,73,93].factored_by_gcf(1) => [14, 19, 36, 46]
|
18
22
|
|
19
23
|
== REQUIREMENTS:
|
20
24
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "greatest_common_factor"
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.2"
|
4
4
|
s.date = "2008-09-19"
|
5
5
|
s.summary = "Find the greatest common factor between sets of numbers. Can also factor the array by the gcf."
|
6
6
|
s.email = "brian@emphaticsolutions.com"
|
@@ -2,8 +2,8 @@ module BrianDoll
|
|
2
2
|
module GreatestCommonFactor
|
3
3
|
|
4
4
|
# Factor this all-integer array by the GCF and return the result
|
5
|
-
def factored_by_gcf
|
6
|
-
gcf = greatest_common_factor
|
5
|
+
def factored_by_gcf(tolerance=0)
|
6
|
+
gcf = greatest_common_factor(tolerance)
|
7
7
|
unless gcf.nil?
|
8
8
|
self.collect { |e| e / gcf }
|
9
9
|
else
|
@@ -12,12 +12,12 @@ module BrianDoll
|
|
12
12
|
end
|
13
13
|
|
14
14
|
# Determine the GCF across each element of this all-integer array
|
15
|
-
def greatest_common_factor
|
15
|
+
def greatest_common_factor(tolerance=0)
|
16
16
|
gcf = nil
|
17
17
|
if all_ints? self
|
18
18
|
dupe_self = self.dup
|
19
19
|
dupe_self.delete(0)
|
20
|
-
factor_sets = find_factor_sets(dupe_self)
|
20
|
+
factor_sets = find_factor_sets(dupe_self, tolerance)
|
21
21
|
ampersand_sets = ""
|
22
22
|
factor_sets.each_with_index do |set,i|
|
23
23
|
if i == 0
|
@@ -49,17 +49,18 @@ module BrianDoll
|
|
49
49
|
private
|
50
50
|
|
51
51
|
# Find all factors for each element in the array
|
52
|
-
def find_factor_sets(collection)
|
53
|
-
collection.
|
52
|
+
def find_factor_sets(collection,tolerance)
|
53
|
+
smallest_number = collection.sort.first
|
54
|
+
collection.collect { |n| find_all_factors(n,smallest_number,tolerance) }
|
54
55
|
end
|
55
56
|
|
56
57
|
# Brute force approach to finding all factors for an integer
|
57
|
-
def find_all_factors(n)
|
58
|
+
def find_all_factors(n,starting_with=n,tolerance=0)
|
58
59
|
n = n.to_i
|
59
|
-
factor = n - 1
|
60
|
+
factor = (n.eql? starting_with) ? (n - 1) : starting_with
|
60
61
|
factors = []
|
61
|
-
|
62
|
-
if ((n % factor)
|
62
|
+
while factor > 1
|
63
|
+
if ((n % factor) <= tolerance)
|
63
64
|
factors << factor
|
64
65
|
end
|
65
66
|
factor -= 1
|