Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/integer.rb

Overview

Ruby Library to Calculate Large Binomials Copyright © 2013 Filip van Laenen <[email protected]>

This file is part of the Ruby Library to Calculate Large Binomials.

The Ruby Library to Calculate Large Binomials is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The Ruby Library to Calculate Large Binomials is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You can find a copy of the GNU General Public License in /LICENSE

Instance Method Summary collapse

Instance Method Details

#binomial(k) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/core_ext/integer.rb', line 26

def binomial(k)
  binomial = breaking_binomial_by_product_of_divisions(k)
  if binomial.nil?
    large_float_binomial_by_product_of_divisions(k)
  else
    binomial
  end
end

#binomial_by_division_of_parallel_products(k) ⇒ Object

Parallel version of binomial_by_division_of_products Note that threads in Ruby 1.9 don’t spread over multiple processors, so due to context switching this parallel version will often be a bit slower than the sequential one.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/core_ext/integer.rb', line 79

def binomial_by_division_of_parallel_products(k)
  # n!/(n-k)!
  top_thread = Thread.new do
    Thread.current[:output] = (self - k + 1 .. self).inject(1, &:*)
  end
  # k!
  bottom_thread = Thread.new do
    Thread.current[:output] = (2 .. k).inject(1, &:*)
  end
  top_thread.join
  bottom_thread.join
  top_thread[:output] / bottom_thread[:output]
end

#binomial_by_division_of_products(k) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/core_ext/integer.rb', line 67

def binomial_by_division_of_products(k)
  # n!/(n-k)!
  p_top = (self - k + 1 .. self).inject(1, &:*)
  # k!
  p_bottom = (2 .. k).inject(1, &:*)
  p_top / p_bottom
end

#binomial_by_pascals_triangle(k) ⇒ Object

Copied from www.brpreiss.com/books/opus8/html/page459.html (on 7 June 2013)



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/core_ext/integer.rb', line 113

def binomial_by_pascals_triangle(k)
  b = []
  b[0] = 1
  (1 .. self).each do | i |
    b[i] = 1
    j = i - 1
    while j > 0
      b[j] += b[j - 1]
      j -= 1
    end
  end
  b[k]
end

#binomial_by_product_of_divisions(k) ⇒ Object



37
38
39
40
41
# File 'lib/core_ext/integer.rb', line 37

def binomial_by_product_of_divisions(k)
  (0...k).reduce(1) do |m, i|
    (m * (self - i)) / (i + 1)
  end
end

#breaking_binomial_by_product_of_divisions(k) ⇒ Object

Breaking version of binomial_by_product_of_divisions



44
45
46
47
48
49
# File 'lib/core_ext/integer.rb', line 44

def breaking_binomial_by_product_of_divisions(k)
  (0...k).reduce(1) do |m, i|
    return nil if m > Float::MAX
    (m * (self - i)) / (i + 1)
  end
end

#float_binomial_by_division_of_products(k) ⇒ Object

Float version of binomial_by_division_of_products



103
104
105
106
107
108
109
# File 'lib/core_ext/integer.rb', line 103

def float_binomial_by_division_of_products(k)
  # n!/(n-k)!
  p_top = (self - k + 1 .. self).inject(1.to_f, &:*)
  # k!
  p_bottom = (2 .. k).inject(1.to_f, &:*)
  p_top / p_bottom
end

#float_binomial_by_product_of_divisions(k) ⇒ Object

Float version of binomial_by_product_of_divisions



59
60
61
62
63
# File 'lib/core_ext/integer.rb', line 59

def float_binomial_by_product_of_divisions(k)
  (0...k).reduce(1.to_f) do |m, i|
    (m * (self - i)) / (i + 1)
  end
end

#large_float_binomial_by_division_of_products(k) ⇒ Object

LargeFloat version of binomial_by_division_of_products



94
95
96
97
98
99
100
# File 'lib/core_ext/integer.rb', line 94

def large_float_binomial_by_division_of_products(k)
  # n!/(n-k)!
  p_top = (self - k + 1 .. self).inject(1.to_lf, &:*)
  # k!
  p_bottom = (2 .. k).inject(1.to_lf, &:*)
  p_top / p_bottom
end

#large_float_binomial_by_product_of_divisions(k) ⇒ Object

LargeFloat version of binomial_by_product_of_divisions



52
53
54
55
56
# File 'lib/core_ext/integer.rb', line 52

def large_float_binomial_by_product_of_divisions(k)
  (0...k).reduce(1.to_lf) do |m, i|
    (m * (self - i)) / (i + 1)
  end
end

#to_lfObject



22
23
24
# File 'lib/core_ext/integer.rb', line 22

def to_lf
  LargeBinomials::LargeFloat.new(self)
end