Module: MissingMath::Integer
- Included in:
- Integer
- Defined in:
- lib/missing_math.rb
Overview
Methods that apply to integers
Instance Method Summary collapse
-
#factorial ⇒ Object
Calculates an integer’s factorial.
-
#factors(include_one = false) ⇒ Object
Returns an array of an integer’s factors.
-
#hexagon ⇒ Object
Returns the hexagonal number.
-
#pentagon ⇒ Object
Returns the pentagonal number.
-
#pentagonal? ⇒ Boolean
Checks if the number is pentagonal.
-
#prime? ⇒ Boolean
Returns boolean true|false if integer is prime.
-
#prime_factors(force_new = false) ⇒ Object
Returns an array of the integer’s prime factors.
-
#triangle ⇒ Object
Returns the triangle number.
-
#triangular? ⇒ Boolean
Checks if the number is triangular.
Instance Method Details
#factorial ⇒ Object
Calculates an integer’s factorial
153 154 155 156 157 158 159 160 |
# File 'lib/missing_math.rb', line 153 def factorial throw "Not an Integer" if !self.is_i? if self == 0 1 else self.downto(1).reduce(:*) end end |
#factors(include_one = false) ⇒ Object
Returns an array of an integer’s factors
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/missing_math.rb', line 164 def factors(include_one=false) throw "Not an Integer" if !self.is_i? last = self i = include_one ? 1 : 2 a = [] while i < last if self % i == 0 last = self / i a << i a << last end i += 1 end return a.sort end |
#hexagon ⇒ Object
Returns the hexagonal number
201 202 203 |
# File 'lib/missing_math.rb', line 201 def hexagon return self * ((2 * self) - 1) end |
#pentagon ⇒ Object
Returns the pentagonal number
196 197 198 |
# File 'lib/missing_math.rb', line 196 def pentagon return (self * ((3 * self) - 1)) / 2 end |
#pentagonal? ⇒ Boolean
Checks if the number is pentagonal. If true, returns the number, otherwise false
217 218 219 220 221 222 223 224 |
# File 'lib/missing_math.rb', line 217 def pentagonal? n = (Math.sqrt((24 * self) + 1) + 1) / 6 if n.floor == n.ceil return n else return false end end |
#prime? ⇒ Boolean
Returns boolean true|false if integer is prime
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/missing_math.rb', line 135 def prime? throw "Not an Integer" if !self.is_i? begin last = Math.sqrt(self).floor i = 2 while i <= last if self % i == 0 return false end i += 1 end return true rescue false end end |
#prime_factors(force_new = false) ⇒ Object
Returns an array of the integer’s prime factors
182 183 184 185 186 187 |
# File 'lib/missing_math.rb', line 182 def prime_factors(force_new=false) ceil = (self / 2).ceil primes = MissingMath.esieve(ceil, force_new) factors = primes.collect { |i| i if self % i == 0 && i <= ceil } return factors.compact.uniq end |
#triangle ⇒ Object
Returns the triangle number
191 192 193 |
# File 'lib/missing_math.rb', line 191 def triangle return (self * (self + 1)) / 2 end |
#triangular? ⇒ Boolean
Checks if the number is triangular. If true, returns the number, otherwise false
207 208 209 210 211 212 213 214 |
# File 'lib/missing_math.rb', line 207 def triangular? n = (Math.sqrt((8 * self) + 1) - 1) / 2 if n.floor == n.ceil return n else return false end end |