factorial-cached 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +13 -3
- data/lib/factorial-cached/Factorial.rb +2 -1
- data/lib/factorial-cached/Factorial.rb~ +94 -0
- metadata +2 -5
- data/lib/Factorial.rb~ +0 -69
- data/lib/Integer.rb~ +0 -7
- data/lib/factorial-cached.rb~ +0 -0
- data/lib/factorial-cached/Integer.rb~ +0 -7
data/README.md
CHANGED
@@ -3,6 +3,8 @@ ruby-factorial-cached
|
|
3
3
|
|
4
4
|
factorial-cached is a gem which aims to fasten your consecutively made factorial operations with caching. This gem was half-way done because of a little Project Euler adventure of mine. Please make any pull requests that you think will fasten the operations and i will check it out and update the gem on rubygems asap.
|
5
5
|
|
6
|
+
Gem also hosted at: [Ruby Gems](https://rubygems.org/gems/factorial-cached)
|
7
|
+
|
6
8
|
Usage
|
7
9
|
=====
|
8
10
|
|
@@ -12,6 +14,14 @@ Usage
|
|
12
14
|
gem install factorial-cached
|
13
15
|
```
|
14
16
|
|
17
|
+
or after cloning / downloading this Github project,
|
18
|
+
|
19
|
+
```
|
20
|
+
cd path/to/project
|
21
|
+
gem build factorial-cached.gemspec
|
22
|
+
gem install factorial-cached-0.0.1.gem
|
23
|
+
```
|
24
|
+
|
15
25
|
### Options
|
16
26
|
|
17
27
|
You can pass some parameters to factorial method to your needs. Above are the options and their default values.
|
@@ -60,9 +70,9 @@ Factorial::toggle(:cache, :cache_none) # Toggles default of caching to none, fur
|
|
60
70
|
Factorial::toggle(:algorithm, :iterative) # Togglest default algorithm to iterative algorithm.
|
61
71
|
Factorial::toggle(:closest, :value) # Closest now returns value of the factorial default.
|
62
72
|
|
63
|
-
5.factorial
|
64
|
-
5.factorial(:cache_one)
|
65
|
-
5.factorial
|
73
|
+
5.factorial #=> Returns 5, caches nothing.
|
74
|
+
5.factorial(:cache_one) #=> Calculates and returns 5!, caches 5! only. (This is the default behaviour of this gem)
|
75
|
+
5.factorial #=> Returns 5! from cache.
|
66
76
|
|
67
77
|
```
|
68
78
|
|
@@ -1,9 +1,10 @@
|
|
1
1
|
class Factorial
|
2
2
|
|
3
3
|
@@cache = {
|
4
|
-
0 => 1, 1 => 1, 2 => 2, 3 => 6, 4 => 24, 5 => 120, 6 => 720, 7 => 5040, 8 => 40320, 9 =>
|
4
|
+
0 => 1, 1 => 1, 2 => 2, 3 => 6, 4 => 24, 5 => 120, 6 => 720, 7 => 5040, 8 => 40320, 9 => 362880, 10 => 3628800, 11 => 39916800, 12 => 479001600, # Below 32 bit
|
5
5
|
14 => 87178291200, 20 => 2432902008176640000, 30 => 265252859812191058636308480000000, 50 => 30414093201713378043612608166064768844377641568960512000000000000, # Milestones for 1-100!
|
6
6
|
70 => 11978571669969891796072783721689098736458938142546425857555362864628009582789845319680000000000000000,
|
7
|
+
85 => 281710411438055027694947944226061159480056634330574206405101912752560026159795933451040286452340924018275123200000000000000000000,
|
7
8
|
100 => 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
|
8
9
|
};
|
9
10
|
|
@@ -0,0 +1,94 @@
|
|
1
|
+
class Factorial
|
2
|
+
|
3
|
+
@@cache = {
|
4
|
+
0 => 1, 1 => 1, 2 => 2, 3 => 6, 4 => 24, 5 => 120, 6 => 720, 7 => 5040, 8 => 40320, 9 => 362880, 10 => 3628800, 11 => 39916800, 12 => 479001600, # Below 32 bit
|
5
|
+
14 => 87178291200, 20 => 2432902008176640000, 30 => 265252859812191058636308480000000, 50 => 30414093201713378043612608166064768844377641568960512000000000000, # Milestones for 1-100!
|
6
|
+
70 => 11978571669969891796072783721689098736458938142546425857555362864628009582789845319680000000000000000,
|
7
|
+
85 => 281710411438055027694947944226061159480056634330574206405101912752560026159795933451040286452340924018275123200000000000000000000,
|
8
|
+
100 => 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
|
9
|
+
};
|
10
|
+
|
11
|
+
@@default = { :cache => :cache_one, :algorithm => :recursive, :closest => :factorial };
|
12
|
+
|
13
|
+
class << self
|
14
|
+
|
15
|
+
def empty_cache( range = nil )
|
16
|
+
if range != nil then
|
17
|
+
range.each{ |key| @@cache.delete(key); };
|
18
|
+
@@cache[0] = 1;
|
19
|
+
else @@cache = {0 => 1}; end
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_cache()
|
23
|
+
return @@cache;
|
24
|
+
end
|
25
|
+
|
26
|
+
def toggle(setting, default)
|
27
|
+
if setting == :cache then
|
28
|
+
@@default[:cache] = default;
|
29
|
+
elsif setting == :algorithm then
|
30
|
+
@@default[:algorithm] = default;
|
31
|
+
elsif setting == :closest then
|
32
|
+
@@default[:closest] = default;
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def closest(number, option = @@default[:closest])
|
37
|
+
close = (@@cache.keys.sort{ |x, y| (x - number).abs <=> (y - number).abs })[0];
|
38
|
+
return option == :value ? @@cache[close] : close;
|
39
|
+
end
|
40
|
+
|
41
|
+
def recursive_factorial(number, cache = @@default[:cache], close = -1)
|
42
|
+
|
43
|
+
return @@cache[number] if close == number;
|
44
|
+
|
45
|
+
if close == -1 then
|
46
|
+
close = self.closest(number, :factorial);
|
47
|
+
end
|
48
|
+
|
49
|
+
result = close < number ? number * recursive_factorial(number - 1, cache, close) : recursive_factorial(number + 1, cache, close) / (number + 1);
|
50
|
+
|
51
|
+
@@cache[number] = result if cache == :cache_all;
|
52
|
+
|
53
|
+
return result;
|
54
|
+
end
|
55
|
+
|
56
|
+
def iterative_factorial(number, cache = @@default[:cache])
|
57
|
+
|
58
|
+
closest = self.closest(number, :factorial);
|
59
|
+
loopThrough = closest < number ? ((closest + 1)..number) : ((number + 1)..closest);
|
60
|
+
result = @@cache[closest];
|
61
|
+
|
62
|
+
for i in loopThrough do
|
63
|
+
|
64
|
+
if closest < number then result *= i; else result /= i; end
|
65
|
+
@@cache[i] = result if cache == :cache_all;
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
return result;
|
70
|
+
end
|
71
|
+
|
72
|
+
def factorial(number, *options)
|
73
|
+
|
74
|
+
return @@cache[number] if @@cache.has_key?(number);
|
75
|
+
return 1 if (number = number.to_i) <= 0 || !number.is_a?(Integer);
|
76
|
+
|
77
|
+
factorial = method(options.include?(:iterative) ? :iterative_factorial :
|
78
|
+
options.include?(:recursive) ? :recursive_factorial :
|
79
|
+
options.include?(:auto) ? (number > 100000 ? :iterative_factorial : :recursive_factorial) :
|
80
|
+
@@default[:algorithm] == :recursive ? :recursive_factorial :
|
81
|
+
@@default[:algorithm] == :auto ? (number > 100000 ? :iterative_factorial : recursive_factorial) : :iterative_factorial);
|
82
|
+
cache = options.include?(:cache_none) ? :cache_none : options.include?(:cache_all) ? :cache_all : options.include?(:cache_one) ? :cache_one : @@default[:cache];
|
83
|
+
|
84
|
+
result = factorial.call(number, cache);
|
85
|
+
|
86
|
+
@@cache[number] = result if cache != :cache_none;
|
87
|
+
|
88
|
+
return result;
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factorial-cached
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -21,12 +21,9 @@ extra_rdoc_files: []
|
|
21
21
|
files:
|
22
22
|
- LICENSE
|
23
23
|
- README.md
|
24
|
-
- lib/factorial-cached.rb~
|
25
|
-
- lib/Factorial.rb~
|
26
24
|
- lib/factorial-cached/Integer.rb
|
27
25
|
- lib/factorial-cached/Factorial.rb
|
28
|
-
- lib/factorial-cached/
|
29
|
-
- lib/Integer.rb~
|
26
|
+
- lib/factorial-cached/Factorial.rb~
|
30
27
|
- lib/factorial-cached.rb
|
31
28
|
homepage: http://rubygems.org/gems/factorial-cached
|
32
29
|
licenses:
|
data/lib/Factorial.rb~
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
class Factorial
|
2
|
-
|
3
|
-
@@cache = {
|
4
|
-
0 => 1, 1 => 1, 2 => 2, 3 => 6, 4 => 24, 5 => 120, 6 => 720, 7 => 5040, 8 => 40320, 9 => 3628800, 10 => 36288000, 11 => 39916800, 12 => 479001600, # Below 32 bit
|
5
|
-
14 => 87178291200, 20 => 2432902008176640000, 30 => 265252859812191058636308480000000, 50 => 30414093201713378043612608166064768844377641568960512000000000000, # Below are milestones for 1-100!
|
6
|
-
70 => 11978571669969891796072783721689098736458938142546425857555362864628009582789845319680000000000000000,
|
7
|
-
100 => 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
|
8
|
-
};
|
9
|
-
|
10
|
-
class << self
|
11
|
-
|
12
|
-
def closest(number, option = :factorial)
|
13
|
-
close = (@@cache.keys.sort{ |x, y| (x - number).abs <=> (y - number).abs })[0];
|
14
|
-
return option == :value ? @@cache[close] : close;
|
15
|
-
end
|
16
|
-
|
17
|
-
def recursive_factorial(number, cache = :cache, close = -1)
|
18
|
-
|
19
|
-
return @@cache[number] if number == close;
|
20
|
-
|
21
|
-
if close == -1 then
|
22
|
-
close = self.closest(number);
|
23
|
-
puts "Using Cache: " + close.to_s
|
24
|
-
end
|
25
|
-
|
26
|
-
result = close < number ? number * recursive_factorial(number - 1, cache, close) : recursive_factorial(number + 1, cache, close) / (number + 1);
|
27
|
-
|
28
|
-
@@cache[number] = result if cache == :cache_all;
|
29
|
-
|
30
|
-
return result;
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
def iterative_factorial(number, cache = :cache)
|
35
|
-
|
36
|
-
closest = self.closest(number);
|
37
|
-
puts "Using Cache: " + close.to_s
|
38
|
-
loopThrough = closest < number ? ((closest + 1)..number) : ((number + 1)..closest);
|
39
|
-
result = @@cache[closest];
|
40
|
-
|
41
|
-
for i in loopThrough do
|
42
|
-
|
43
|
-
if closest < number then result *= i; else result /= i; end
|
44
|
-
@@cache[i] = result if cache == :cache_all;
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
return result;
|
49
|
-
end
|
50
|
-
|
51
|
-
def factorial(number, *options)
|
52
|
-
|
53
|
-
return @@cache[number] if @@cache.has_key?(number);
|
54
|
-
return 1 if number <= 0;
|
55
|
-
|
56
|
-
factorial = method(options.include?(:iterative) || number > 100000 ? :iterative_factorial : :recursive_factorial);
|
57
|
-
cache = options.include?(:cache_none) ? :cache_none : options.include?(:cache_all) ? :cache_all : :cache;
|
58
|
-
|
59
|
-
result = factorial.call(number, cache);
|
60
|
-
|
61
|
-
@@cache[number] = result if cache != :cache_none;
|
62
|
-
|
63
|
-
return factorial;
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
data/lib/Integer.rb~
DELETED
data/lib/factorial-cached.rb~
DELETED
File without changes
|