missing-math 0.1.3 → 0.1.4

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.
@@ -1,4 +1,7 @@
1
1
  module MissingMath
2
+ # @esieve
3
+ # @fib
4
+
2
5
 
3
6
  # Methods that can apply to all number object types
4
7
  module Number
@@ -13,6 +16,19 @@ module MissingMath
13
16
  end
14
17
  end
15
18
 
19
+ # Converts an number to an array of numbers. If a decimal number, the decimal will be a position in the array
20
+ # Example: 12345.to_a => [1, 2, 3, 4, 5]
21
+ def to_a
22
+ self.to_s.split('').map do |n|
23
+ matches = n.scan(/[0-9]/).length
24
+ if matches > 0
25
+ n.to_i
26
+ else
27
+ n
28
+ end
29
+ end
30
+ end
31
+
16
32
  # Returns the number with a number added to the beginning of the number
17
33
  # @param integer n The number to prepend
18
34
  # Example: 2345.prepend(1) => 12345
@@ -55,6 +71,18 @@ module MissingMath
55
71
  number_out(str)
56
72
  end
57
73
 
74
+ # Checks if the number contains a digit sequence or regex
75
+ # @param integer|regexp search
76
+ # Example 1: 12345.contains?(34) => true
77
+ # Example 2: 12345.contains?(/34/) => true
78
+ def contains?(search)
79
+ if search.is_a? Regexp
80
+ return self.to_s.scan(search).length > 0 ? true : false
81
+ else
82
+ return self.to_s.index(search.to_s) ? true : false
83
+ end
84
+ end
85
+
58
86
  # Returns the number with the first digit moved to the end
59
87
  # @param boolean right Defaults to rotating right. Set to false to rotate left
60
88
  # Example 1: 12345.rotate => 23451
@@ -70,31 +98,21 @@ module MissingMath
70
98
  number_out(str)
71
99
  end
72
100
 
73
- # Checks if the number contains a digit sequence or regex
74
- # @param integer|regexp search
75
- # Example 1: 12345.contains?(34) => true
76
- # Example 2: 12345.contains?(/34/) => true
77
- def contains?(search)
78
- if search.is_a? Regexp
79
- return self.to_s.scan(search).length > 0 ? true : false
80
- else
81
- return self.to_s.index(search.to_s) ? true : false
82
- end
101
+ # Returns the number reversed
102
+ # Example: 12345.reverse => 54321
103
+ def reverse
104
+ a = self.to_a
105
+ str = a.reverse.join('')
106
+ number_out(str)
83
107
  end
84
108
 
85
- # Converts an number to an array of numbers. If a decimal number, the decimal will be a position in the array
86
- # Example: 12345.to_a => [1, 2, 3, 4, 5]
87
- def to_a
88
- self.to_s.split('').map do |n|
89
- matches = n.scan(/[0-9]/).length
90
- if matches > 0
91
- n.to_i
92
- else
93
- n
94
- end
95
- end
109
+ # Returns true|false if the number is a palindrome
110
+ # Example: 123454321.palindrome? => true
111
+ def palindrome?
112
+ return self == self.reverse
96
113
  end
97
114
 
115
+
98
116
  private
99
117
 
100
118
  # Output a stringed version of a number as a float or integer (whichever it deserves)
@@ -134,7 +152,11 @@ module MissingMath
134
152
  # Calculates an integer's factorial
135
153
  def factorial
136
154
  throw "Not an Integer" if !self.is_i?
137
- self.downto(1).reduce(:*)
155
+ if self == 0
156
+ 1
157
+ else
158
+ self.downto(1).reduce(:*)
159
+ end
138
160
  end
139
161
 
140
162
  # Returns an array of an integer's factors
@@ -163,6 +185,44 @@ module MissingMath
163
185
  factors = primes.collect { |i| i if self % i == 0 && i <= ceil }
164
186
  return factors.compact.uniq
165
187
  end
188
+
189
+
190
+ # Returns the triangle number
191
+ def triangle
192
+ return (self * (self + 1)) / 2
193
+ end
194
+
195
+ # Returns the pentagonal number
196
+ def pentagon
197
+ return (self * ((3 * self) - 1)) / 2
198
+ end
199
+
200
+ # Returns the hexagonal number
201
+ def hexagon
202
+ return self * ((2 * self) - 1)
203
+ end
204
+
205
+
206
+ # Checks if the number is triangular. If true, returns the number, otherwise false
207
+ def triangular?
208
+ n = (Math.sqrt((8 * self) + 1) - 1) / 2
209
+ if n.floor == n.ceil
210
+ return n
211
+ else
212
+ return false
213
+ end
214
+ end
215
+
216
+ # Checks if the number is pentagonal. If true, returns the number, otherwise false
217
+ def pentagonal?
218
+ n = (Math.sqrt((24 * self) + 1) + 1) / 6
219
+ if n.floor == n.ceil
220
+ return n
221
+ else
222
+ return false
223
+ end
224
+ end
225
+
166
226
  end
167
227
 
168
228
 
@@ -188,6 +248,21 @@ module MissingMath
188
248
  return @esieve
189
249
  end
190
250
 
251
+ # Generates an array holding an n-length fibonacci sequence
252
+ # @param integer n Length of sequence to generate
253
+ # @param boolean true_fib Use the true fibonacci sequence (0, 1, 1, 2...) or skip the first two values (1, 2, 3, 5...). Default true
254
+ # @param boolean force_new Force new module variable @fib generation. Default uses module variable @fib if it hasn't been set
255
+ # Example: MissingMath.fibonacci(5) => [0, 1, 1, 2, 3]
256
+ def self.fibonacci(n, true_fib=true, force_new=false)
257
+ if !@fib || force_new
258
+ @fib = true_fib ? [0, 1] : [1, 2]
259
+ while @fib.length < n
260
+ @fib << @fib[-1] + @fib[-2]
261
+ end
262
+ end
263
+ return @fib
264
+ end
265
+
191
266
  end
192
267
 
193
268
 
@@ -195,3 +270,4 @@ end
195
270
  require 'missing_math/integer'
196
271
  require 'missing_math/float'
197
272
  require 'missing_math/fixnum'
273
+ require 'missing_math/array'
@@ -0,0 +1,7 @@
1
+ class Array
2
+
3
+ def to_i
4
+ i = self.join('').to_i
5
+ end
6
+
7
+ end
@@ -1,3 +1,3 @@
1
1
  module MissingMath
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: missing-math
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-04 00:00:00.000000000 Z
12
+ date: 2013-12-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A library of missing math functions.
15
15
  email: mail@enorganik.com
@@ -17,6 +17,7 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - lib/missing_math/array.rb
20
21
  - lib/missing_math/fixnum.rb
21
22
  - lib/missing_math/float.rb
22
23
  - lib/missing_math/integer.rb