factorial 0.0.3 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -3
- data/README.rdoc +9 -3
- data/bin/factorial +2 -2
- data/bin/gcf +7 -0
- data/lib/factorial.rb +11 -3
- data/lib/factorial/gcf.rb +30 -0
- data/lib/factorial/result.rb +3 -2
- data/spec/factorial_spec.rb +7 -0
- metadata +3 -1
data/README.md
CHANGED
@@ -3,7 +3,7 @@ Factorial
|
|
3
3
|
|
4
4
|
Description
|
5
5
|
------------
|
6
|
-
Factorial helps you to calculate the factorial of a number. e.g factorial of 5 = 120.
|
6
|
+
Factorial helps you to calculate the factorial of a number ora list of numbers. e.g factorial of 5 = 120. It has also the features to find the greatest commpn factor (GCF) of the series of numbers.
|
7
7
|
|
8
8
|
Requirements
|
9
9
|
------------
|
@@ -24,6 +24,10 @@ Usage
|
|
24
24
|
|
25
25
|
### For determining the factorial of a number
|
26
26
|
|
27
|
-
fact = Factorial::Base.new(
|
27
|
+
fact = Factorial::Base.new(numbers) e.g 2, 5
|
28
28
|
|
29
|
-
fact.factorial_result
|
29
|
+
fact.factorial_result => 2, 120
|
30
|
+
|
31
|
+
### For determining the GCF
|
32
|
+
|
33
|
+
fact.gcf_result => 1
|
data/README.rdoc
CHANGED
@@ -3,7 +3,7 @@ Factorial
|
|
3
3
|
|
4
4
|
Description
|
5
5
|
------------
|
6
|
-
Factorial helps you to calculate the factorial of a number. e.g factorial of 5 = 120.
|
6
|
+
Factorial helps you to calculate the factorial of a number or a list of numbers. e.g factorial of 5 = 120. It has also the features to find the greatest common factor (GCF) of the series of numbers.
|
7
7
|
|
8
8
|
Requirements
|
9
9
|
------------
|
@@ -24,6 +24,12 @@ Usage
|
|
24
24
|
|
25
25
|
### For determining the factorial of a number
|
26
26
|
|
27
|
-
fact = Factorial::Base.new(
|
27
|
+
fact = Factorial::Base.new(numbers) e.g 2, 5
|
28
28
|
|
29
|
-
fact.factorial_result
|
29
|
+
fact.factorial_result => 2, 120
|
30
|
+
|
31
|
+
### For determining the GCF
|
32
|
+
|
33
|
+
fact.gcf_result => 1
|
34
|
+
|
35
|
+
|
data/bin/factorial
CHANGED
data/bin/gcf
ADDED
data/lib/factorial.rb
CHANGED
@@ -1,11 +1,19 @@
|
|
1
1
|
require 'factorial/result'
|
2
|
+
require 'factorial/gcf'
|
3
|
+
|
2
4
|
module Factorial
|
3
5
|
|
4
6
|
class Base
|
5
|
-
attr_reader :factorial_result
|
7
|
+
attr_reader :factorial_result, :gcf_result
|
8
|
+
|
9
|
+
def initialize(*args)
|
10
|
+
@factorial_result = Factorial::Result.new(*args)
|
11
|
+
@gcf_result = Factorial::Gcf.new(*args)
|
12
|
+
end
|
6
13
|
|
7
|
-
def
|
8
|
-
@
|
14
|
+
def greatest_common_factor
|
15
|
+
@gcf = Factorial::Gcf.new(*args)
|
9
16
|
end
|
17
|
+
|
10
18
|
end
|
11
19
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Factorial
|
2
|
+
|
3
|
+
class Gcf < Array
|
4
|
+
def initialize(*number_array)
|
5
|
+
@num_arr = *number_array
|
6
|
+
self << manage_the_array
|
7
|
+
end
|
8
|
+
|
9
|
+
def manage_the_array
|
10
|
+
gcd = 1
|
11
|
+
(0..@num_arr.length - 1).each do |i|
|
12
|
+
gcd = gcd_inner_method(@num_arr[i], @num_arr[i + 1])
|
13
|
+
@num_arr[i + 1] = gcd
|
14
|
+
end
|
15
|
+
gcd
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
def gcd_inner_method(n1, n2)
|
20
|
+
r = 1
|
21
|
+
while r > 0 && n2
|
22
|
+
r = n1 % n2
|
23
|
+
n1 = n2
|
24
|
+
n2 = r
|
25
|
+
end
|
26
|
+
n1
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/lib/factorial/result.rb
CHANGED
data/spec/factorial_spec.rb
CHANGED
@@ -10,4 +10,11 @@ describe Factorial::Base do
|
|
10
10
|
fact = Factorial::Base.new(0)
|
11
11
|
fact.factorial_result.first.should == 1
|
12
12
|
end
|
13
|
+
|
14
|
+
it "checking the factorial of multiple numbers" do
|
15
|
+
fact = Factorial::Base.new(5,0,8)
|
16
|
+
fact.factorial_result[0].should == 120
|
17
|
+
fact.factorial_result[1].should == 1
|
18
|
+
fact.factorial_result[2].should == 40320
|
19
|
+
end
|
13
20
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factorial
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -20,9 +20,11 @@ extra_rdoc_files: []
|
|
20
20
|
files:
|
21
21
|
- lib/factorial.rb
|
22
22
|
- lib/factorial/result.rb
|
23
|
+
- lib/factorial/gcf.rb
|
23
24
|
- spec/spec_helper.rb
|
24
25
|
- spec/factorial_spec.rb
|
25
26
|
- bin/factorial
|
27
|
+
- bin/gcf
|
26
28
|
- ./README.md
|
27
29
|
- ./README.rdoc
|
28
30
|
homepage: http://rubygems.org/gems/factorial
|