missing-math 0.1.2 → 0.1.3
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 +21 -16
- data/lib/missing_math/version.rb +1 -1
- metadata +2 -2
data/lib/missing_math.rb
CHANGED
@@ -138,8 +138,8 @@ module MissingMath
|
|
138
138
|
end
|
139
139
|
|
140
140
|
# Returns an array of an integer's factors
|
141
|
-
# @param boolean include_one
|
142
|
-
def factors(include_one=
|
141
|
+
# @param boolean include_one Default is to exclude the number 1 from the output; to include set to false
|
142
|
+
def factors(include_one=false)
|
143
143
|
throw "Not an Integer" if !self.is_i?
|
144
144
|
last = self
|
145
145
|
i = include_one ? 1 : 2
|
@@ -156,11 +156,12 @@ module MissingMath
|
|
156
156
|
end
|
157
157
|
|
158
158
|
# Returns an array of the integer's prime factors
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
159
|
+
# @param boolean force_new Force new module variable @esieve generation. Default uses module variable @esieve if it hasn't been set
|
160
|
+
def prime_factors(force_new=false)
|
161
|
+
ceil = (self / 2).ceil
|
162
|
+
primes = MissingMath.esieve(ceil, force_new)
|
163
|
+
factors = primes.collect { |i| i if self % i == 0 && i <= ceil }
|
164
|
+
return factors.compact.uniq
|
164
165
|
end
|
165
166
|
end
|
166
167
|
|
@@ -170,17 +171,21 @@ module MissingMath
|
|
170
171
|
|
171
172
|
# Generates a prime sieve with max value n
|
172
173
|
# @param integer n Max value of sieve
|
174
|
+
# @param boolean force_new Force new module variable @esieve generation. Default uses module variable @esieve if it hasn't been set
|
173
175
|
# Example: MissingMath.esieve(1000) => [2, 3, 5, ...]
|
174
|
-
def self.esieve(n)
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
176
|
+
def self.esieve(n, force_new=false)
|
177
|
+
if !@esieve || force_new
|
178
|
+
a = (0..n).to_a
|
179
|
+
a[0] = nil
|
180
|
+
a[1] = nil
|
181
|
+
a.each do |i|
|
182
|
+
next unless i
|
183
|
+
break if i * i > n
|
184
|
+
(i * i).step(n, i) { |m| a[m] = nil}
|
185
|
+
end
|
186
|
+
@esieve = a.compact
|
182
187
|
end
|
183
|
-
return
|
188
|
+
return @esieve
|
184
189
|
end
|
185
190
|
|
186
191
|
end
|
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.3
|
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-
|
12
|
+
date: 2013-12-04 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A library of missing math functions.
|
15
15
|
email: mail@enorganik.com
|