backports 3.6.7 → 3.6.8
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/backports/1.9.1/stdlib/prime.rb +150 -181
- data/lib/backports/version.rb +1 -1
- data/test/_backport_guards_test.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4e7cf14929385f92c1246d5da12ac424b422338e
|
|
4
|
+
data.tar.gz: ddccf768a48e4cf4327cb2dd4244d14932b9d0bb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 62b66d725bbd40b4462ef16500c78d8d19a8382e09fbeb834ddbd2eab4a7df9bca45d22382604f91d557481b35873977fc304f266fe29be58c9602803f4db6ce
|
|
7
|
+
data.tar.gz: 551bddeb601f9eccbbb92469914ef3c7a7a1bd0777f30ec5be5f0763aa3a5b543dbf70fa4159a9ecdd4f7514a632379106af88b685caa03ead26038d88e7cf70
|
data/Gemfile.lock
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
# frozen_string_literal: false
|
|
1
2
|
#
|
|
2
3
|
# = prime.rb
|
|
3
4
|
#
|
|
@@ -29,9 +30,16 @@ class Integer
|
|
|
29
30
|
Prime.prime_division(self, generator)
|
|
30
31
|
end
|
|
31
32
|
|
|
32
|
-
# Returns true if +self+ is a prime number,
|
|
33
|
+
# Returns true if +self+ is a prime number, else returns false.
|
|
33
34
|
def prime?
|
|
34
|
-
|
|
35
|
+
return self >= 2 if self <= 3
|
|
36
|
+
return false if self % 2 == 0 or self % 3 == 0
|
|
37
|
+
(5..(self**0.5).floor).step(6).each do |i|
|
|
38
|
+
if self % i == 0 || self % (i + 2) == 0
|
|
39
|
+
return false
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
true
|
|
35
43
|
end
|
|
36
44
|
|
|
37
45
|
# Iterates the given block over all prime numbers.
|
|
@@ -46,57 +54,52 @@ end
|
|
|
46
54
|
# The set of all prime numbers.
|
|
47
55
|
#
|
|
48
56
|
# == Example
|
|
49
|
-
#
|
|
50
|
-
#
|
|
51
|
-
#
|
|
57
|
+
#
|
|
58
|
+
# Prime.each(100) do |prime|
|
|
59
|
+
# p prime #=> 2, 3, 5, 7, 11, ...., 97
|
|
60
|
+
# end
|
|
61
|
+
#
|
|
62
|
+
# Prime is Enumerable:
|
|
63
|
+
#
|
|
64
|
+
# Prime.first 5 # => [2, 3, 5, 7, 11]
|
|
52
65
|
#
|
|
53
66
|
# == Retrieving the instance
|
|
54
|
-
# +Prime+.new is obsolete. Now +Prime+ has the default instance and you can
|
|
55
|
-
# access it as +Prime+.instance.
|
|
56
67
|
#
|
|
57
68
|
# For convenience, each instance method of +Prime+.instance can be accessed
|
|
58
69
|
# as a class method of +Prime+.
|
|
59
70
|
#
|
|
60
71
|
# e.g.
|
|
61
|
-
#
|
|
62
|
-
#
|
|
72
|
+
# Prime.instance.prime?(2) #=> true
|
|
73
|
+
# Prime.prime?(2) #=> true
|
|
63
74
|
#
|
|
64
75
|
# == Generators
|
|
76
|
+
#
|
|
65
77
|
# A "generator" provides an implementation of enumerating pseudo-prime
|
|
66
78
|
# numbers and it remembers the position of enumeration and upper bound.
|
|
67
|
-
#
|
|
68
|
-
# compatible
|
|
79
|
+
# Furthermore, it is an external iterator of prime enumeration which is
|
|
80
|
+
# compatible with an Enumerator.
|
|
69
81
|
#
|
|
70
82
|
# +Prime+::+PseudoPrimeGenerator+ is the base class for generators.
|
|
71
83
|
# There are few implementations of generator.
|
|
72
84
|
#
|
|
73
85
|
# [+Prime+::+EratosthenesGenerator+]
|
|
74
|
-
# Uses eratosthenes'
|
|
86
|
+
# Uses eratosthenes' sieve.
|
|
75
87
|
# [+Prime+::+TrialDivisionGenerator+]
|
|
76
88
|
# Uses the trial division method.
|
|
77
89
|
# [+Prime+::+Generator23+]
|
|
78
|
-
# Generates all positive integers which
|
|
90
|
+
# Generates all positive integers which are not divisible by either 2 or 3.
|
|
79
91
|
# This sequence is very bad as a pseudo-prime sequence. But this
|
|
80
|
-
# is faster and uses much less memory than other generators. So,
|
|
92
|
+
# is faster and uses much less memory than the other generators. So,
|
|
81
93
|
# it is suitable for factorizing an integer which is not large but
|
|
82
94
|
# has many prime factors. e.g. for Prime#prime? .
|
|
95
|
+
|
|
83
96
|
class Prime
|
|
84
|
-
unless Prime.const_defined? :OldCompatibility
|
|
85
97
|
include Enumerable
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
# obsolete. Use +Prime+::+instance+ or class methods of +Prime+.
|
|
89
|
-
def initialize
|
|
90
|
-
@generator = EratosthenesGenerator.new
|
|
91
|
-
extend OldCompatibility
|
|
92
|
-
warn "Prime::new is obsolete. use Prime::instance or class methods of Prime."
|
|
93
|
-
end
|
|
98
|
+
include Singleton
|
|
94
99
|
|
|
95
100
|
class << self
|
|
96
101
|
extend Forwardable
|
|
97
102
|
include Enumerable
|
|
98
|
-
# Returns the default instance of Prime.
|
|
99
|
-
def instance; @the_instance end
|
|
100
103
|
|
|
101
104
|
def method_added(method) # :nodoc:
|
|
102
105
|
(class<< self;self;end).def_delegator :instance, method
|
|
@@ -106,6 +109,7 @@ unless Prime.const_defined? :OldCompatibility
|
|
|
106
109
|
# Iterates the given block over all prime numbers.
|
|
107
110
|
#
|
|
108
111
|
# == Parameters
|
|
112
|
+
#
|
|
109
113
|
# +ubound+::
|
|
110
114
|
# Optional. An arbitrary positive number.
|
|
111
115
|
# The upper bound of enumeration. The method enumerates
|
|
@@ -114,40 +118,37 @@ unless Prime.const_defined? :OldCompatibility
|
|
|
114
118
|
# Optional. An implementation of pseudo-prime generator.
|
|
115
119
|
#
|
|
116
120
|
# == Return value
|
|
121
|
+
#
|
|
117
122
|
# An evaluated value of the given block at the last time.
|
|
118
123
|
# Or an enumerator which is compatible to an +Enumerator+
|
|
119
124
|
# if no block given.
|
|
120
125
|
#
|
|
121
126
|
# == Description
|
|
127
|
+
#
|
|
122
128
|
# Calls +block+ once for each prime number, passing the prime as
|
|
123
129
|
# a parameter.
|
|
124
130
|
#
|
|
125
131
|
# +ubound+::
|
|
126
|
-
# Upper bound of prime numbers. The iterator stops after
|
|
132
|
+
# Upper bound of prime numbers. The iterator stops after it
|
|
127
133
|
# yields all prime numbers p <= +ubound+.
|
|
128
134
|
#
|
|
129
|
-
# == Note
|
|
130
|
-
# +Prime+.+new+ returns a object extended by +Prime+::+OldCompatibility+
|
|
131
|
-
# in order to compatibility to Ruby 1.8, and +Prime+#each is overwritten
|
|
132
|
-
# by +Prime+::+OldCompatibility+#+each+.
|
|
133
|
-
#
|
|
134
|
-
# +Prime+.+new+ is now obsolete. Use +Prime+.+instance+.+each+ or simply
|
|
135
|
-
# +Prime+.+each+.
|
|
136
135
|
def each(ubound = nil, generator = EratosthenesGenerator.new, &block)
|
|
137
136
|
generator.upper_bound = ubound
|
|
138
137
|
generator.each(&block)
|
|
139
138
|
end
|
|
140
139
|
|
|
141
140
|
|
|
142
|
-
# Returns true if +value+ is prime,
|
|
141
|
+
# Returns true if +value+ is a prime number, else returns false.
|
|
143
142
|
#
|
|
144
143
|
# == Parameters
|
|
144
|
+
#
|
|
145
145
|
# +value+:: an arbitrary integer to be checked.
|
|
146
146
|
# +generator+:: optional. A pseudo-prime generator.
|
|
147
147
|
def prime?(value, generator = Prime::Generator23.new)
|
|
148
|
-
|
|
148
|
+
raise ArgumentError, "Expected a prime generator, got #{generator}" unless generator.respond_to? :each
|
|
149
|
+
raise ArgumentError, "Expected an integer, got #{value}" unless value.respond_to?(:integer?) && value.integer?
|
|
149
150
|
return false if value < 2
|
|
150
|
-
|
|
151
|
+
generator.each do |num|
|
|
151
152
|
q,r = value.divmod num
|
|
152
153
|
return true if q < num
|
|
153
154
|
return false if r == 0
|
|
@@ -162,13 +163,14 @@ unless Prime.const_defined? :OldCompatibility
|
|
|
162
163
|
# and a natural number -- an exponent.
|
|
163
164
|
#
|
|
164
165
|
# == Example
|
|
165
|
-
# For [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]]
|
|
166
|
-
# p_1**e_1 * p_2**e_2 * .... * p_n**e_n.
|
|
166
|
+
# For <tt>[[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]]</tt>, it returns:
|
|
167
167
|
#
|
|
168
|
-
#
|
|
168
|
+
# p_1**e_1 * p_2**e_2 * .... * p_n**e_n.
|
|
169
|
+
#
|
|
170
|
+
# Prime.int_from_prime_division([[2,2], [3,1]]) #=> 12
|
|
169
171
|
def int_from_prime_division(pd)
|
|
170
172
|
pd.inject(1){|value, (prime, index)|
|
|
171
|
-
value
|
|
173
|
+
value * prime**index
|
|
172
174
|
}
|
|
173
175
|
end
|
|
174
176
|
|
|
@@ -178,22 +180,25 @@ unless Prime.const_defined? :OldCompatibility
|
|
|
178
180
|
# +value+:: An arbitrary integer.
|
|
179
181
|
# +generator+:: Optional. A pseudo-prime generator.
|
|
180
182
|
# +generator+.succ must return the next
|
|
181
|
-
# pseudo-prime number in the
|
|
183
|
+
# pseudo-prime number in the ascending
|
|
182
184
|
# order. It must generate all prime numbers,
|
|
183
|
-
# but may generate non prime numbers.
|
|
185
|
+
# but may also generate non prime numbers too.
|
|
184
186
|
#
|
|
185
187
|
# === Exceptions
|
|
186
188
|
# +ZeroDivisionError+:: when +value+ is zero.
|
|
187
189
|
#
|
|
188
190
|
# == Example
|
|
189
|
-
# For an arbitrary integer
|
|
190
|
-
#
|
|
191
|
-
#
|
|
192
|
-
#
|
|
191
|
+
# For an arbitrary integer:
|
|
192
|
+
#
|
|
193
|
+
# n = p_1**e_1 * p_2**e_2 * .... * p_n**e_n,
|
|
194
|
+
#
|
|
195
|
+
# prime_division(n) returns:
|
|
193
196
|
#
|
|
194
|
-
#
|
|
197
|
+
# [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]].
|
|
195
198
|
#
|
|
196
|
-
|
|
199
|
+
# Prime.prime_division(12) #=> [[2,2], [3,1]]
|
|
200
|
+
#
|
|
201
|
+
def prime_division(value, generator = Prime::Generator23.new)
|
|
197
202
|
raise ZeroDivisionError if value == 0
|
|
198
203
|
if value < 0
|
|
199
204
|
value = -value
|
|
@@ -201,22 +206,22 @@ unless Prime.const_defined? :OldCompatibility
|
|
|
201
206
|
else
|
|
202
207
|
pv = []
|
|
203
208
|
end
|
|
204
|
-
|
|
209
|
+
generator.each do |prime|
|
|
205
210
|
count = 0
|
|
206
211
|
while (value1, mod = value.divmod(prime)
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
212
|
+
mod) == 0
|
|
213
|
+
value = value1
|
|
214
|
+
count += 1
|
|
210
215
|
end
|
|
211
216
|
if count != 0
|
|
212
|
-
|
|
217
|
+
pv.push [prime, count]
|
|
213
218
|
end
|
|
214
219
|
break if value1 <= prime
|
|
215
220
|
end
|
|
216
221
|
if value > 1
|
|
217
222
|
pv.push [value, 1]
|
|
218
223
|
end
|
|
219
|
-
|
|
224
|
+
pv
|
|
220
225
|
end
|
|
221
226
|
|
|
222
227
|
# An abstract class for enumerating pseudo-prime numbers.
|
|
@@ -256,33 +261,45 @@ unless Prime.const_defined? :OldCompatibility
|
|
|
256
261
|
raise NotImplementedError, "need to define `rewind'"
|
|
257
262
|
end
|
|
258
263
|
|
|
259
|
-
# Iterates the given block for each prime
|
|
260
|
-
def each
|
|
261
|
-
return self.dup unless
|
|
264
|
+
# Iterates the given block for each prime number.
|
|
265
|
+
def each
|
|
266
|
+
return self.dup unless block_given?
|
|
262
267
|
if @ubound
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
268
|
+
last_value = nil
|
|
269
|
+
loop do
|
|
270
|
+
prime = succ
|
|
271
|
+
break last_value if prime > @ubound
|
|
272
|
+
last_value = yield prime
|
|
273
|
+
end
|
|
269
274
|
else
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
275
|
+
loop do
|
|
276
|
+
yield succ
|
|
277
|
+
end
|
|
273
278
|
end
|
|
274
279
|
end
|
|
275
280
|
|
|
276
281
|
# see +Enumerator+#with_index.
|
|
277
|
-
|
|
282
|
+
def with_index(offset = 0)
|
|
283
|
+
return enum_for(:with_index, offset) { Float::INFINITY } unless block_given?
|
|
284
|
+
return each_with_index(&proc) if offset == 0
|
|
285
|
+
|
|
286
|
+
each do |prime|
|
|
287
|
+
yield prime, offset
|
|
288
|
+
offset += 1
|
|
289
|
+
end
|
|
290
|
+
end
|
|
278
291
|
|
|
279
292
|
# see +Enumerator+#with_object.
|
|
280
293
|
def with_object(obj)
|
|
281
|
-
return enum_for(:with_object) unless block_given?
|
|
294
|
+
return enum_for(:with_object, obj) { Float::INFINITY } unless block_given?
|
|
282
295
|
each do |prime|
|
|
283
|
-
|
|
296
|
+
yield prime, obj
|
|
284
297
|
end
|
|
285
298
|
end
|
|
299
|
+
|
|
300
|
+
def size
|
|
301
|
+
Float::INFINITY
|
|
302
|
+
end
|
|
286
303
|
end
|
|
287
304
|
|
|
288
305
|
# An implementation of +PseudoPrimeGenerator+.
|
|
@@ -290,12 +307,13 @@ unless Prime.const_defined? :OldCompatibility
|
|
|
290
307
|
# Uses +EratosthenesSieve+.
|
|
291
308
|
class EratosthenesGenerator < PseudoPrimeGenerator
|
|
292
309
|
def initialize
|
|
293
|
-
@
|
|
310
|
+
@last_prime_index = -1
|
|
294
311
|
super
|
|
295
312
|
end
|
|
296
313
|
|
|
297
314
|
def succ
|
|
298
|
-
@
|
|
315
|
+
@last_prime_index += 1
|
|
316
|
+
EratosthenesSieve.instance.get_nth_prime(@last_prime_index)
|
|
299
317
|
end
|
|
300
318
|
def rewind
|
|
301
319
|
initialize
|
|
@@ -320,11 +338,11 @@ unless Prime.const_defined? :OldCompatibility
|
|
|
320
338
|
alias next succ
|
|
321
339
|
end
|
|
322
340
|
|
|
323
|
-
# Generates all
|
|
324
|
-
# are not
|
|
341
|
+
# Generates all integers which are greater than 2 and
|
|
342
|
+
# are not divisible by either 2 or 3.
|
|
325
343
|
#
|
|
326
344
|
# This is a pseudo-prime generator, suitable on
|
|
327
|
-
# checking primality of
|
|
345
|
+
# checking primality of an integer by brute force
|
|
328
346
|
# method.
|
|
329
347
|
class Generator23<PseudoPrimeGenerator
|
|
330
348
|
def initialize
|
|
@@ -334,19 +352,17 @@ unless Prime.const_defined? :OldCompatibility
|
|
|
334
352
|
end
|
|
335
353
|
|
|
336
354
|
def succ
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
end
|
|
347
|
-
end
|
|
348
|
-
return @prime
|
|
355
|
+
if (@step)
|
|
356
|
+
@prime += @step
|
|
357
|
+
@step = 6 - @step
|
|
358
|
+
else
|
|
359
|
+
case @prime
|
|
360
|
+
when 1; @prime = 2
|
|
361
|
+
when 2; @prime = 3
|
|
362
|
+
when 3; @prime = 5; @step = 2
|
|
363
|
+
end
|
|
349
364
|
end
|
|
365
|
+
@prime
|
|
350
366
|
end
|
|
351
367
|
alias next succ
|
|
352
368
|
def rewind
|
|
@@ -354,9 +370,6 @@ unless Prime.const_defined? :OldCompatibility
|
|
|
354
370
|
end
|
|
355
371
|
end
|
|
356
372
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
373
|
# Internal use. An implementation of prime table by trial division method.
|
|
361
374
|
class TrialDivision
|
|
362
375
|
include Singleton
|
|
@@ -376,7 +389,7 @@ unless Prime.const_defined? :OldCompatibility
|
|
|
376
389
|
|
|
377
390
|
# Returns the cached prime numbers.
|
|
378
391
|
def cache
|
|
379
|
-
|
|
392
|
+
@primes
|
|
380
393
|
end
|
|
381
394
|
alias primes cache
|
|
382
395
|
alias primes_so_far cache
|
|
@@ -386,112 +399,68 @@ unless Prime.const_defined? :OldCompatibility
|
|
|
386
399
|
# +index+ is a 0-based index.
|
|
387
400
|
def [](index)
|
|
388
401
|
while index >= @primes.length
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
402
|
+
# Only check for prime factors up to the square root of the potential primes,
|
|
403
|
+
# but without the performance hit of an actual square root calculation.
|
|
404
|
+
if @next_to_check + 4 > @ulticheck_next_squared
|
|
405
|
+
@ulticheck_index += 1
|
|
406
|
+
@ulticheck_next_squared = @primes.at(@ulticheck_index + 1) ** 2
|
|
407
|
+
end
|
|
408
|
+
# Only check numbers congruent to one and five, modulo six. All others
|
|
409
|
+
|
|
410
|
+
# are divisible by two or three. This also allows us to skip checking against
|
|
411
|
+
# two and three.
|
|
412
|
+
@primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?
|
|
413
|
+
@next_to_check += 4
|
|
414
|
+
@primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?
|
|
415
|
+
@next_to_check += 2
|
|
403
416
|
end
|
|
404
|
-
|
|
417
|
+
@primes[index]
|
|
405
418
|
end
|
|
406
419
|
end
|
|
407
420
|
|
|
408
|
-
# Internal use. An implementation of eratosthenes'
|
|
421
|
+
# Internal use. An implementation of eratosthenes' sieve
|
|
409
422
|
class EratosthenesSieve
|
|
410
423
|
include Singleton
|
|
411
424
|
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
FILLED_ENTRY = (1 << NUMS_PER_ENTRY) - 1
|
|
417
|
-
|
|
418
|
-
def initialize # :nodoc:
|
|
419
|
-
# bitmap for odd prime numbers less than 256.
|
|
420
|
-
# For an arbitrary odd number n, @tables[i][j][k] is
|
|
421
|
-
# * 1 if n is prime,
|
|
422
|
-
# * 0 if n is composite,
|
|
423
|
-
# where i,j,k = indices(n)
|
|
424
|
-
@tables = [[0xcb6e, 0x64b4, 0x129a, 0x816d, 0x4c32, 0x864a, 0x820d, 0x2196].freeze]
|
|
425
|
+
def initialize
|
|
426
|
+
@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
|
|
427
|
+
# @max_checked must be an even number
|
|
428
|
+
@max_checked = @primes.last + 1
|
|
425
429
|
end
|
|
426
430
|
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
n
|
|
430
|
-
table_index, integer_index, bit_index = indices(n)
|
|
431
|
-
loop do
|
|
432
|
-
extend_table until @tables.length > table_index
|
|
433
|
-
for j in integer_index...ENTRIES_PER_TABLE
|
|
434
|
-
if !@tables[table_index][j].zero?
|
|
435
|
-
for k in bit_index...BITS_PER_ENTRY
|
|
436
|
-
return NUMS_PER_TABLE*table_index + NUMS_PER_ENTRY*j + 2*k+1 if !@tables[table_index][j][k].zero?
|
|
437
|
-
end
|
|
438
|
-
end
|
|
439
|
-
bit_index = 0
|
|
440
|
-
end
|
|
441
|
-
table_index += 1; integer_index = 0
|
|
442
|
-
end
|
|
431
|
+
def get_nth_prime(n)
|
|
432
|
+
compute_primes while @primes.size <= n
|
|
433
|
+
@primes[n]
|
|
443
434
|
end
|
|
444
435
|
|
|
445
436
|
private
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
#
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
(start...ubound).step(2*p) do |n|
|
|
469
|
-
_, j, k = indices(n)
|
|
470
|
-
new_table[j] &= FILLED_ENTRY^(1<<k)
|
|
471
|
-
end
|
|
437
|
+
def compute_primes
|
|
438
|
+
# max_segment_size must be an even number
|
|
439
|
+
max_segment_size = 1e6.to_i
|
|
440
|
+
max_cached_prime = @primes.last
|
|
441
|
+
# do not double count primes if #compute_primes is interrupted
|
|
442
|
+
# by Timeout.timeout
|
|
443
|
+
@max_checked = max_cached_prime + 1 if max_cached_prime > @max_checked
|
|
444
|
+
|
|
445
|
+
segment_min = @max_checked
|
|
446
|
+
segment_max = [segment_min + max_segment_size, max_cached_prime * 2].min
|
|
447
|
+
root = Integer(Math.sqrt(segment_max).floor)
|
|
448
|
+
|
|
449
|
+
segment = ((segment_min + 1) .. segment_max).step(2).to_a
|
|
450
|
+
|
|
451
|
+
(1..Float::INFINITY).each do |sieving|
|
|
452
|
+
prime = @primes[sieving]
|
|
453
|
+
break if prime > root
|
|
454
|
+
composite_index = (-(segment_min + 1 + prime) / 2) % prime
|
|
455
|
+
while composite_index < segment.size do
|
|
456
|
+
segment[composite_index] = nil
|
|
457
|
+
composite_index += prime
|
|
458
|
+
end
|
|
472
459
|
end
|
|
473
|
-
@tables << new_table.freeze
|
|
474
|
-
end
|
|
475
|
-
end
|
|
476
460
|
|
|
477
|
-
|
|
478
|
-
module OldCompatibility
|
|
479
|
-
# Returns the next prime number and forwards internal pointer.
|
|
480
|
-
def succ
|
|
481
|
-
@generator.succ
|
|
482
|
-
end
|
|
483
|
-
alias next succ
|
|
461
|
+
@primes.concat(segment.compact!)
|
|
484
462
|
|
|
485
|
-
|
|
486
|
-
#
|
|
487
|
-
# Iterates the given block over all prime numbers. Note that enumeration starts from
|
|
488
|
-
# the current position of internal pointer, not rewound.
|
|
489
|
-
def each(&block)
|
|
490
|
-
return @generator.dup unless block_given?
|
|
491
|
-
loop do
|
|
492
|
-
yield succ
|
|
493
|
-
end
|
|
463
|
+
@max_checked = segment_max
|
|
494
464
|
end
|
|
495
465
|
end
|
|
496
466
|
end
|
|
497
|
-
end
|
data/lib/backports/version.rb
CHANGED
|
@@ -35,14 +35,14 @@ class AAA_TestBackportGuards < Test::Unit::TestCase
|
|
|
35
35
|
EXCLUDE.map!(&:to_sym) if instance_methods.first.is_a?(Symbol)
|
|
36
36
|
|
|
37
37
|
# For some very strange reason, Hash[kvp.flatten] doesn't always work in 1.8.6??
|
|
38
|
-
def
|
|
38
|
+
def to_hash(key_value_pairs)
|
|
39
39
|
h = {}
|
|
40
40
|
key_value_pairs.each{|k,v| h[k] = v}
|
|
41
41
|
h
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
def class_signature(klass)
|
|
45
|
-
|
|
45
|
+
to_hash(
|
|
46
46
|
(klass.instance_methods - EXCLUDE).map{|m| [m, klass.instance_method(m)] } +
|
|
47
47
|
(klass.methods - EXCLUDE).map{|m| [".#{m}", klass.method(m) ]}
|
|
48
48
|
)
|
|
@@ -62,7 +62,7 @@ class AAA_TestBackportGuards < Test::Unit::TestCase
|
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
def digest
|
|
65
|
-
|
|
65
|
+
to_hash(
|
|
66
66
|
CLASSES.map { |klass| [klass, class_signature(klass)] }
|
|
67
67
|
)
|
|
68
68
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: backports
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.6.
|
|
4
|
+
version: 3.6.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Marc-André Lafortune
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2016-02-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: Essential backports that enable many of the nice features of Ruby 1.8.7
|
|
14
14
|
up to 2.1.0 for earlier versions.
|