math_probability 0.1.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: 512ad96cd9913406ad58ea4fbeefa4d09e13032f
4
- data.tar.gz: 857404e2821e0b4b0dec8077340b35f2b87d0a17
3
+ metadata.gz: 126b15b6b5d89b948f88d15ed649551fca41dd58
4
+ data.tar.gz: a6cbc805e89281180ccd401ed840c6652adf11be
5
5
  SHA512:
6
- metadata.gz: 606b30f62503ca92d8804887298bab08304db167417a5617fe9dd2be18080349ececf8da413fb9d19ce8c55ed6dce7a1ff2f516eef5079d0baf17f63cb99f503
7
- data.tar.gz: 12649f47c02bc83d8eab0a36cddaf90d2ac91fa7489b068da1186b6d1ca88d56bd328ffa7782b5712a355e6acd0e57dbe3925fcd1219b4dbbd1758e788cece09
6
+ metadata.gz: 8aaf432baa59badd5b8e441aafdbae9bf24e88ef1724c332d008601e9fdac24e993bacfdd904e52db183638fa2df437d7a5c5eb8415890f6a0eb05c84d848452
7
+ data.tar.gz: b5ec7a3931ec40893e0c0ddb64b519b4d2d34970132317a6b875cc9eeb10c05235e2fd39686c670e5dfbbfe4535b5ec596fc20cb00f33b361e43e2ab2f8f70a7
@@ -1,3 +1,3 @@
1
1
  module MathProbability
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -27,10 +27,22 @@ module MathProbability
27
27
  (((objects.factoral)/((objects-at_a_time).factoral))*(1.0/(at_a_time.factoral))).to_i
28
28
  end
29
29
 
30
- def self.probability(choice, outcomes)
30
+ def self.probability(choices, outcomes, reduce=true)
31
31
  answer ||= []
32
- a = choice.to_f / outcomes
33
- answer << a << "#{a*100}%" << "#{choice}/#{outcomes}"
32
+ a = choices.to_f / outcomes
33
+ answer << a << "#{a*100}%" << (reduce ? reduced_fraction(choices, outcomes) : "#{choices}/#{outcomes}")
34
+ end
35
+
36
+ private
37
+
38
+ def self.reduced_fraction(nominator, denominator)
39
+ lcd ||= denominator
40
+ denominator.downto(2).each do |n|
41
+ if nominator % n == 0 && denominator % n == 0 && n != denominator && n < lcd
42
+ lcd = n
43
+ end
44
+ end
45
+ lcd != denominator ? "#{nominator/lcd}/#{denominator/lcd}" : "#{nominator}/#{denominator}"
34
46
  end
35
47
 
36
48
  end
@@ -31,6 +31,14 @@ describe MathProbability::Probability do
31
31
  it "should return all forms of Probability" do
32
32
  subject.probability(1,2).must_equal([0.5, "50.0%", "1/2"])
33
33
  end
34
+
35
+ it "should return a reduced fraction" do
36
+ subject.probability(2,4).must_equal([0.5, "50.0%", "1/2"])
37
+ end
38
+
39
+ it "should not return a reduced fraction if flag is set to false" do
40
+ subject.probability(2,4,false).must_equal([0.5, "50.0%", "2/4"])
41
+ end
34
42
 
35
43
  end
36
44
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: math_probability
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Race