missing-math 0.1.1 → 0.1.2
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.
- data/lib/missing_math.rb +173 -27
- data/lib/missing_math/fixnum.rb +6 -0
- data/lib/missing_math/float.rb +6 -0
- data/lib/missing_math/integer.rb +7 -1
- data/lib/missing_math/version.rb +1 -1
- metadata +2 -2
data/lib/missing_math.rb
CHANGED
@@ -1,46 +1,192 @@
|
|
1
1
|
module MissingMath
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
# Methods that can apply to all number object types
|
4
|
+
module Number
|
5
|
+
# Returns the number of digits in the number (if a float, decimal is excluded)
|
6
|
+
# Example: 12345.length => 5
|
7
|
+
def length
|
8
|
+
str = self.to_s
|
9
|
+
if str.index('.')
|
10
|
+
str.length - 1
|
11
|
+
elsif
|
12
|
+
str.length
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns the number with a number added to the beginning of the number
|
17
|
+
# @param integer n The number to prepend
|
18
|
+
# Example: 2345.prepend(1) => 12345
|
19
|
+
def prepend(n)
|
20
|
+
str = "#{n}#{self}"
|
21
|
+
number_out(str)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns the number with a number added to the end of the number
|
25
|
+
# @param integer n The number to append
|
26
|
+
# Example: 1234.append(5) => 12345
|
27
|
+
def append(n)
|
28
|
+
str = "#{self}#{n}"
|
29
|
+
number_out(str)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns the number with the first digit removed from the beginning of the number
|
33
|
+
# Similar to an Array#shift method
|
34
|
+
# Example: 12345.shift => 2345
|
35
|
+
def shift
|
36
|
+
str = self.to_s[1..-1]
|
37
|
+
number_out(str)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns the number with the last digit removed from the end of the number
|
41
|
+
# Similar to an Array#pop method
|
42
|
+
# Example: 12345.pop => 1234
|
43
|
+
def pop
|
44
|
+
str = self.to_s[0..-2]
|
45
|
+
number_out(str)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns a digit subset/substring of the current number with a 0-indexed integer or range
|
49
|
+
# @param integer|range search The substring search to return
|
50
|
+
# Similar to a String object's substring methods.
|
51
|
+
# Example 1: 12345.substr(2) => 3
|
52
|
+
# Example 2: 12345.substr(0..2) => 123
|
53
|
+
def substr(search)
|
54
|
+
str = self.to_s[search]
|
55
|
+
number_out(str)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Returns the number with the first digit moved to the end
|
59
|
+
# @param boolean right Defaults to rotating right. Set to false to rotate left
|
60
|
+
# Example 1: 12345.rotate => 23451
|
61
|
+
# Example 2: 12345.rotate(false) => 51234
|
62
|
+
def rotate(right=true)
|
63
|
+
a = self.to_a
|
64
|
+
if right
|
65
|
+
a.push(a.shift)
|
66
|
+
elsif !right
|
67
|
+
a.unshift(a.pop)
|
68
|
+
end
|
69
|
+
str = a.join('')
|
70
|
+
number_out(str)
|
71
|
+
end
|
72
|
+
|
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
|
83
|
+
end
|
84
|
+
|
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
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
# Output a stringed version of a number as a float or integer (whichever it deserves)
|
101
|
+
def number_out(str)
|
102
|
+
i = str.to_i
|
103
|
+
f = str.to_f
|
104
|
+
if i == f
|
105
|
+
return i
|
106
|
+
else
|
107
|
+
return f
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
# Methods that apply to integers
|
115
|
+
module Integer
|
116
|
+
# Returns boolean true|false if integer is prime
|
117
|
+
def prime?
|
118
|
+
throw "Not an Integer" if !self.is_i?
|
119
|
+
begin
|
120
|
+
last = Math.sqrt(self).floor
|
121
|
+
i = 2
|
122
|
+
while i <= last
|
123
|
+
if self % i == 0
|
124
|
+
return false
|
125
|
+
end
|
126
|
+
i += 1
|
127
|
+
end
|
128
|
+
return true
|
129
|
+
rescue
|
130
|
+
false
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# Calculates an integer's factorial
|
135
|
+
def factorial
|
136
|
+
throw "Not an Integer" if !self.is_i?
|
137
|
+
self.downto(1).reduce(:*)
|
138
|
+
end
|
139
|
+
|
140
|
+
# Returns an array of an integer's factors
|
141
|
+
# @param boolean include_one To exclude the number 1 from the output, set to false
|
142
|
+
def factors(include_one=true)
|
143
|
+
throw "Not an Integer" if !self.is_i?
|
144
|
+
last = self
|
145
|
+
i = include_one ? 1 : 2
|
146
|
+
a = []
|
147
|
+
while i < last
|
9
148
|
if self % i == 0
|
10
|
-
|
149
|
+
last = self / i
|
150
|
+
a << i
|
151
|
+
a << last
|
11
152
|
end
|
12
153
|
i += 1
|
13
154
|
end
|
14
|
-
return
|
15
|
-
rescue
|
16
|
-
false
|
155
|
+
return a.sort
|
17
156
|
end
|
18
|
-
end
|
19
157
|
|
20
|
-
|
21
|
-
|
22
|
-
|
158
|
+
# Returns an array of the integer's prime factors
|
159
|
+
def prime_factors
|
160
|
+
ceil = (self / 2).floor
|
161
|
+
primes = MissingMath.esieve(ceil)
|
162
|
+
factors = primes.collect { |i| self % i == 0 }
|
163
|
+
return factors
|
164
|
+
end
|
23
165
|
end
|
24
166
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
167
|
+
|
168
|
+
|
169
|
+
### Module methods
|
170
|
+
|
171
|
+
# Generates a prime sieve with max value n
|
172
|
+
# @param integer n Max value of sieve
|
173
|
+
# Example: MissingMath.esieve(1000) => [2, 3, 5, ...]
|
174
|
+
def self.esieve(n)
|
175
|
+
a = (0..n).to_a
|
176
|
+
a[0] = nil
|
177
|
+
a[1] = nil
|
178
|
+
a.each do |i|
|
179
|
+
next unless i
|
180
|
+
break if i * i > n
|
181
|
+
(i * i).step(n, i) { |m| a[m] = nil}
|
37
182
|
end
|
38
|
-
return a.
|
183
|
+
return a.compact
|
39
184
|
end
|
40
185
|
|
41
186
|
end
|
42
187
|
|
43
188
|
|
189
|
+
|
44
190
|
require 'missing_math/integer'
|
45
191
|
require 'missing_math/float'
|
46
192
|
require 'missing_math/fixnum'
|
data/lib/missing_math/fixnum.rb
CHANGED
data/lib/missing_math/float.rb
CHANGED
data/lib/missing_math/integer.rb
CHANGED
data/lib/missing_math/version.rb
CHANGED
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.
|
4
|
+
version: 0.1.2
|
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
|
+
date: 2013-12-02 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A library of missing math functions.
|
15
15
|
email: mail@enorganik.com
|