Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby30/share/ruby
File: prime.rb
# frozen_string_literal: false
[0] Fix | Delete
#
[1] Fix | Delete
# = prime.rb
[2] Fix | Delete
#
[3] Fix | Delete
# Prime numbers and factorization library.
[4] Fix | Delete
#
[5] Fix | Delete
# Copyright::
[6] Fix | Delete
# Copyright (c) 1998-2008 Keiju ISHITSUKA(SHL Japan Inc.)
[7] Fix | Delete
# Copyright (c) 2008 Yuki Sonoda (Yugui) <yugui@yugui.jp>
[8] Fix | Delete
#
[9] Fix | Delete
# Documentation::
[10] Fix | Delete
# Yuki Sonoda
[11] Fix | Delete
#
[12] Fix | Delete
[13] Fix | Delete
require "singleton"
[14] Fix | Delete
require "forwardable"
[15] Fix | Delete
[16] Fix | Delete
class Integer
[17] Fix | Delete
# Re-composes a prime factorization and returns the product.
[18] Fix | Delete
#
[19] Fix | Delete
# See Prime#int_from_prime_division for more details.
[20] Fix | Delete
def Integer.from_prime_division(pd)
[21] Fix | Delete
Prime.int_from_prime_division(pd)
[22] Fix | Delete
end
[23] Fix | Delete
[24] Fix | Delete
# Returns the factorization of +self+.
[25] Fix | Delete
#
[26] Fix | Delete
# See Prime#prime_division for more details.
[27] Fix | Delete
def prime_division(generator = Prime::Generator23.new)
[28] Fix | Delete
Prime.prime_division(self, generator)
[29] Fix | Delete
end
[30] Fix | Delete
[31] Fix | Delete
# Returns true if +self+ is a prime number, else returns false.
[32] Fix | Delete
# Not recommended for very big integers (> 10**23).
[33] Fix | Delete
def prime?
[34] Fix | Delete
return self >= 2 if self <= 3
[35] Fix | Delete
[36] Fix | Delete
if (bases = miller_rabin_bases)
[37] Fix | Delete
return miller_rabin_test(bases)
[38] Fix | Delete
end
[39] Fix | Delete
[40] Fix | Delete
return true if self == 5
[41] Fix | Delete
return false unless 30.gcd(self) == 1
[42] Fix | Delete
(7..Integer.sqrt(self)).step(30) do |p|
[43] Fix | Delete
return false if
[44] Fix | Delete
self%(p) == 0 || self%(p+4) == 0 || self%(p+6) == 0 || self%(p+10) == 0 ||
[45] Fix | Delete
self%(p+12) == 0 || self%(p+16) == 0 || self%(p+22) == 0 || self%(p+24) == 0
[46] Fix | Delete
end
[47] Fix | Delete
true
[48] Fix | Delete
end
[49] Fix | Delete
[50] Fix | Delete
MILLER_RABIN_BASES = [
[51] Fix | Delete
[2],
[52] Fix | Delete
[2,3],
[53] Fix | Delete
[31,73],
[54] Fix | Delete
[2,3,5],
[55] Fix | Delete
[2,3,5,7],
[56] Fix | Delete
[2,7,61],
[57] Fix | Delete
[2,13,23,1662803],
[58] Fix | Delete
[2,3,5,7,11],
[59] Fix | Delete
[2,3,5,7,11,13],
[60] Fix | Delete
[2,3,5,7,11,13,17],
[61] Fix | Delete
[2,3,5,7,11,13,17,19,23],
[62] Fix | Delete
[2,3,5,7,11,13,17,19,23,29,31,37],
[63] Fix | Delete
[2,3,5,7,11,13,17,19,23,29,31,37,41],
[64] Fix | Delete
].map!(&:freeze).freeze
[65] Fix | Delete
private_constant :MILLER_RABIN_BASES
[66] Fix | Delete
[67] Fix | Delete
private def miller_rabin_bases
[68] Fix | Delete
# Miller-Rabin's complexity is O(k log^3n).
[69] Fix | Delete
# So we can reduce the complexity by reducing the number of bases tested.
[70] Fix | Delete
# Using values from https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
[71] Fix | Delete
i = case
[72] Fix | Delete
when self < 0xffff then
[73] Fix | Delete
# For small integers, Miller Rabin can be slower
[74] Fix | Delete
# There is no mathematical significance to 0xffff
[75] Fix | Delete
return nil
[76] Fix | Delete
# when self < 2_047 then 0
[77] Fix | Delete
when self < 1_373_653 then 1
[78] Fix | Delete
when self < 9_080_191 then 2
[79] Fix | Delete
when self < 25_326_001 then 3
[80] Fix | Delete
when self < 3_215_031_751 then 4
[81] Fix | Delete
when self < 4_759_123_141 then 5
[82] Fix | Delete
when self < 1_122_004_669_633 then 6
[83] Fix | Delete
when self < 2_152_302_898_747 then 7
[84] Fix | Delete
when self < 3_474_749_660_383 then 8
[85] Fix | Delete
when self < 341_550_071_728_321 then 9
[86] Fix | Delete
when self < 3_825_123_056_546_413_051 then 10
[87] Fix | Delete
when self < 318_665_857_834_031_151_167_461 then 11
[88] Fix | Delete
when self < 3_317_044_064_679_887_385_961_981 then 12
[89] Fix | Delete
else return nil
[90] Fix | Delete
end
[91] Fix | Delete
MILLER_RABIN_BASES[i]
[92] Fix | Delete
end
[93] Fix | Delete
[94] Fix | Delete
private def miller_rabin_test(bases)
[95] Fix | Delete
return false if even?
[96] Fix | Delete
[97] Fix | Delete
r = 0
[98] Fix | Delete
d = self >> 1
[99] Fix | Delete
while d.even?
[100] Fix | Delete
d >>= 1
[101] Fix | Delete
r += 1
[102] Fix | Delete
end
[103] Fix | Delete
[104] Fix | Delete
self_minus_1 = self-1
[105] Fix | Delete
bases.each do |a|
[106] Fix | Delete
x = a.pow(d, self)
[107] Fix | Delete
next if x == 1 || x == self_minus_1 || a == self
[108] Fix | Delete
[109] Fix | Delete
return false if r.times do
[110] Fix | Delete
x = x.pow(2, self)
[111] Fix | Delete
break if x == self_minus_1
[112] Fix | Delete
end
[113] Fix | Delete
end
[114] Fix | Delete
true
[115] Fix | Delete
end
[116] Fix | Delete
[117] Fix | Delete
# Iterates the given block over all prime numbers.
[118] Fix | Delete
#
[119] Fix | Delete
# See +Prime+#each for more details.
[120] Fix | Delete
def Integer.each_prime(ubound, &block) # :yields: prime
[121] Fix | Delete
Prime.each(ubound, &block)
[122] Fix | Delete
end
[123] Fix | Delete
end
[124] Fix | Delete
[125] Fix | Delete
#
[126] Fix | Delete
# The set of all prime numbers.
[127] Fix | Delete
#
[128] Fix | Delete
# == Example
[129] Fix | Delete
#
[130] Fix | Delete
# Prime.each(100) do |prime|
[131] Fix | Delete
# p prime #=> 2, 3, 5, 7, 11, ...., 97
[132] Fix | Delete
# end
[133] Fix | Delete
#
[134] Fix | Delete
# Prime is Enumerable:
[135] Fix | Delete
#
[136] Fix | Delete
# Prime.first 5 # => [2, 3, 5, 7, 11]
[137] Fix | Delete
#
[138] Fix | Delete
# == Retrieving the instance
[139] Fix | Delete
#
[140] Fix | Delete
# For convenience, each instance method of +Prime+.instance can be accessed
[141] Fix | Delete
# as a class method of +Prime+.
[142] Fix | Delete
#
[143] Fix | Delete
# e.g.
[144] Fix | Delete
# Prime.instance.prime?(2) #=> true
[145] Fix | Delete
# Prime.prime?(2) #=> true
[146] Fix | Delete
#
[147] Fix | Delete
# == Generators
[148] Fix | Delete
#
[149] Fix | Delete
# A "generator" provides an implementation of enumerating pseudo-prime
[150] Fix | Delete
# numbers and it remembers the position of enumeration and upper bound.
[151] Fix | Delete
# Furthermore, it is an external iterator of prime enumeration which is
[152] Fix | Delete
# compatible with an Enumerator.
[153] Fix | Delete
#
[154] Fix | Delete
# +Prime+::+PseudoPrimeGenerator+ is the base class for generators.
[155] Fix | Delete
# There are few implementations of generator.
[156] Fix | Delete
#
[157] Fix | Delete
# [+Prime+::+EratosthenesGenerator+]
[158] Fix | Delete
# Uses Eratosthenes' sieve.
[159] Fix | Delete
# [+Prime+::+TrialDivisionGenerator+]
[160] Fix | Delete
# Uses the trial division method.
[161] Fix | Delete
# [+Prime+::+Generator23+]
[162] Fix | Delete
# Generates all positive integers which are not divisible by either 2 or 3.
[163] Fix | Delete
# This sequence is very bad as a pseudo-prime sequence. But this
[164] Fix | Delete
# is faster and uses much less memory than the other generators. So,
[165] Fix | Delete
# it is suitable for factorizing an integer which is not large but
[166] Fix | Delete
# has many prime factors. e.g. for Prime#prime? .
[167] Fix | Delete
[168] Fix | Delete
class Prime
[169] Fix | Delete
[170] Fix | Delete
VERSION = "0.1.2"
[171] Fix | Delete
[172] Fix | Delete
include Enumerable
[173] Fix | Delete
include Singleton
[174] Fix | Delete
[175] Fix | Delete
class << self
[176] Fix | Delete
extend Forwardable
[177] Fix | Delete
include Enumerable
[178] Fix | Delete
[179] Fix | Delete
def method_added(method) # :nodoc:
[180] Fix | Delete
(class<< self;self;end).def_delegator :instance, method
[181] Fix | Delete
end
[182] Fix | Delete
end
[183] Fix | Delete
[184] Fix | Delete
# Iterates the given block over all prime numbers.
[185] Fix | Delete
#
[186] Fix | Delete
# == Parameters
[187] Fix | Delete
#
[188] Fix | Delete
# +ubound+::
[189] Fix | Delete
# Optional. An arbitrary positive number.
[190] Fix | Delete
# The upper bound of enumeration. The method enumerates
[191] Fix | Delete
# prime numbers infinitely if +ubound+ is nil.
[192] Fix | Delete
# +generator+::
[193] Fix | Delete
# Optional. An implementation of pseudo-prime generator.
[194] Fix | Delete
#
[195] Fix | Delete
# == Return value
[196] Fix | Delete
#
[197] Fix | Delete
# An evaluated value of the given block at the last time.
[198] Fix | Delete
# Or an enumerator which is compatible to an +Enumerator+
[199] Fix | Delete
# if no block given.
[200] Fix | Delete
#
[201] Fix | Delete
# == Description
[202] Fix | Delete
#
[203] Fix | Delete
# Calls +block+ once for each prime number, passing the prime as
[204] Fix | Delete
# a parameter.
[205] Fix | Delete
#
[206] Fix | Delete
# +ubound+::
[207] Fix | Delete
# Upper bound of prime numbers. The iterator stops after it
[208] Fix | Delete
# yields all prime numbers p <= +ubound+.
[209] Fix | Delete
#
[210] Fix | Delete
def each(ubound = nil, generator = EratosthenesGenerator.new, &block)
[211] Fix | Delete
generator.upper_bound = ubound
[212] Fix | Delete
generator.each(&block)
[213] Fix | Delete
end
[214] Fix | Delete
[215] Fix | Delete
# Returns true if +obj+ is an Integer and is prime. Also returns
[216] Fix | Delete
# true if +obj+ is a Module that is an ancestor of +Prime+.
[217] Fix | Delete
# Otherwise returns false.
[218] Fix | Delete
def include?(obj)
[219] Fix | Delete
case obj
[220] Fix | Delete
when Integer
[221] Fix | Delete
prime?(obj)
[222] Fix | Delete
when Module
[223] Fix | Delete
Module.instance_method(:include?).bind(Prime).call(obj)
[224] Fix | Delete
else
[225] Fix | Delete
false
[226] Fix | Delete
end
[227] Fix | Delete
end
[228] Fix | Delete
[229] Fix | Delete
# Returns true if +value+ is a prime number, else returns false.
[230] Fix | Delete
# Integer#prime? is much more performant.
[231] Fix | Delete
#
[232] Fix | Delete
# == Parameters
[233] Fix | Delete
#
[234] Fix | Delete
# +value+:: an arbitrary integer to be checked.
[235] Fix | Delete
# +generator+:: optional. A pseudo-prime generator.
[236] Fix | Delete
def prime?(value, generator = Prime::Generator23.new)
[237] Fix | Delete
raise ArgumentError, "Expected a prime generator, got #{generator}" unless generator.respond_to? :each
[238] Fix | Delete
raise ArgumentError, "Expected an integer, got #{value}" unless value.respond_to?(:integer?) && value.integer?
[239] Fix | Delete
return false if value < 2
[240] Fix | Delete
generator.each do |num|
[241] Fix | Delete
q,r = value.divmod num
[242] Fix | Delete
return true if q < num
[243] Fix | Delete
return false if r == 0
[244] Fix | Delete
end
[245] Fix | Delete
end
[246] Fix | Delete
[247] Fix | Delete
# Re-composes a prime factorization and returns the product.
[248] Fix | Delete
#
[249] Fix | Delete
# For the decomposition:
[250] Fix | Delete
#
[251] Fix | Delete
# [[p_1, e_1], [p_2, e_2], ..., [p_n, e_n]],
[252] Fix | Delete
#
[253] Fix | Delete
# it returns:
[254] Fix | Delete
#
[255] Fix | Delete
# p_1**e_1 * p_2**e_2 * ... * p_n**e_n.
[256] Fix | Delete
#
[257] Fix | Delete
# == Parameters
[258] Fix | Delete
# +pd+:: Array of pairs of integers.
[259] Fix | Delete
# Each pair consists of a prime number -- a prime factor --
[260] Fix | Delete
# and a natural number -- its exponent (multiplicity).
[261] Fix | Delete
#
[262] Fix | Delete
# == Example
[263] Fix | Delete
# Prime.int_from_prime_division([[3, 2], [5, 1]]) #=> 45
[264] Fix | Delete
# 3**2 * 5 #=> 45
[265] Fix | Delete
#
[266] Fix | Delete
def int_from_prime_division(pd)
[267] Fix | Delete
pd.inject(1){|value, (prime, index)|
[268] Fix | Delete
value * prime**index
[269] Fix | Delete
}
[270] Fix | Delete
end
[271] Fix | Delete
[272] Fix | Delete
# Returns the factorization of +value+.
[273] Fix | Delete
#
[274] Fix | Delete
# For an arbitrary integer:
[275] Fix | Delete
#
[276] Fix | Delete
# p_1**e_1 * p_2**e_2 * ... * p_n**e_n,
[277] Fix | Delete
#
[278] Fix | Delete
# prime_division returns an array of pairs of integers:
[279] Fix | Delete
#
[280] Fix | Delete
# [[p_1, e_1], [p_2, e_2], ..., [p_n, e_n]].
[281] Fix | Delete
#
[282] Fix | Delete
# Each pair consists of a prime number -- a prime factor --
[283] Fix | Delete
# and a natural number -- its exponent (multiplicity).
[284] Fix | Delete
#
[285] Fix | Delete
# == Parameters
[286] Fix | Delete
# +value+:: An arbitrary integer.
[287] Fix | Delete
# +generator+:: Optional. A pseudo-prime generator.
[288] Fix | Delete
# +generator+.succ must return the next
[289] Fix | Delete
# pseudo-prime number in ascending order.
[290] Fix | Delete
# It must generate all prime numbers,
[291] Fix | Delete
# but may also generate non-prime numbers, too.
[292] Fix | Delete
#
[293] Fix | Delete
# === Exceptions
[294] Fix | Delete
# +ZeroDivisionError+:: when +value+ is zero.
[295] Fix | Delete
#
[296] Fix | Delete
# == Example
[297] Fix | Delete
#
[298] Fix | Delete
# Prime.prime_division(45) #=> [[3, 2], [5, 1]]
[299] Fix | Delete
# 3**2 * 5 #=> 45
[300] Fix | Delete
#
[301] Fix | Delete
def prime_division(value, generator = Prime::Generator23.new)
[302] Fix | Delete
raise ZeroDivisionError if value == 0
[303] Fix | Delete
if value < 0
[304] Fix | Delete
value = -value
[305] Fix | Delete
pv = [[-1, 1]]
[306] Fix | Delete
else
[307] Fix | Delete
pv = []
[308] Fix | Delete
end
[309] Fix | Delete
generator.each do |prime|
[310] Fix | Delete
count = 0
[311] Fix | Delete
while (value1, mod = value.divmod(prime)
[312] Fix | Delete
mod) == 0
[313] Fix | Delete
value = value1
[314] Fix | Delete
count += 1
[315] Fix | Delete
end
[316] Fix | Delete
if count != 0
[317] Fix | Delete
pv.push [prime, count]
[318] Fix | Delete
end
[319] Fix | Delete
break if value1 <= prime
[320] Fix | Delete
end
[321] Fix | Delete
if value > 1
[322] Fix | Delete
pv.push [value, 1]
[323] Fix | Delete
end
[324] Fix | Delete
pv
[325] Fix | Delete
end
[326] Fix | Delete
[327] Fix | Delete
# An abstract class for enumerating pseudo-prime numbers.
[328] Fix | Delete
#
[329] Fix | Delete
# Concrete subclasses should override succ, next, rewind.
[330] Fix | Delete
class PseudoPrimeGenerator
[331] Fix | Delete
include Enumerable
[332] Fix | Delete
[333] Fix | Delete
def initialize(ubound = nil)
[334] Fix | Delete
@ubound = ubound
[335] Fix | Delete
end
[336] Fix | Delete
[337] Fix | Delete
def upper_bound=(ubound)
[338] Fix | Delete
@ubound = ubound
[339] Fix | Delete
end
[340] Fix | Delete
def upper_bound
[341] Fix | Delete
@ubound
[342] Fix | Delete
end
[343] Fix | Delete
[344] Fix | Delete
# returns the next pseudo-prime number, and move the internal
[345] Fix | Delete
# position forward.
[346] Fix | Delete
#
[347] Fix | Delete
# +PseudoPrimeGenerator+#succ raises +NotImplementedError+.
[348] Fix | Delete
def succ
[349] Fix | Delete
raise NotImplementedError, "need to define `succ'"
[350] Fix | Delete
end
[351] Fix | Delete
[352] Fix | Delete
# alias of +succ+.
[353] Fix | Delete
def next
[354] Fix | Delete
raise NotImplementedError, "need to define `next'"
[355] Fix | Delete
end
[356] Fix | Delete
[357] Fix | Delete
# Rewinds the internal position for enumeration.
[358] Fix | Delete
#
[359] Fix | Delete
# See +Enumerator+#rewind.
[360] Fix | Delete
def rewind
[361] Fix | Delete
raise NotImplementedError, "need to define `rewind'"
[362] Fix | Delete
end
[363] Fix | Delete
[364] Fix | Delete
# Iterates the given block for each prime number.
[365] Fix | Delete
def each
[366] Fix | Delete
return self.dup unless block_given?
[367] Fix | Delete
if @ubound
[368] Fix | Delete
last_value = nil
[369] Fix | Delete
loop do
[370] Fix | Delete
prime = succ
[371] Fix | Delete
break last_value if prime > @ubound
[372] Fix | Delete
last_value = yield prime
[373] Fix | Delete
end
[374] Fix | Delete
else
[375] Fix | Delete
loop do
[376] Fix | Delete
yield succ
[377] Fix | Delete
end
[378] Fix | Delete
end
[379] Fix | Delete
end
[380] Fix | Delete
[381] Fix | Delete
# see +Enumerator+#with_index.
[382] Fix | Delete
def with_index(offset = 0, &block)
[383] Fix | Delete
return enum_for(:with_index, offset) { Float::INFINITY } unless block
[384] Fix | Delete
return each_with_index(&block) if offset == 0
[385] Fix | Delete
[386] Fix | Delete
each do |prime|
[387] Fix | Delete
yield prime, offset
[388] Fix | Delete
offset += 1
[389] Fix | Delete
end
[390] Fix | Delete
end
[391] Fix | Delete
[392] Fix | Delete
# see +Enumerator+#with_object.
[393] Fix | Delete
def with_object(obj)
[394] Fix | Delete
return enum_for(:with_object, obj) { Float::INFINITY } unless block_given?
[395] Fix | Delete
each do |prime|
[396] Fix | Delete
yield prime, obj
[397] Fix | Delete
end
[398] Fix | Delete
end
[399] Fix | Delete
[400] Fix | Delete
def size
[401] Fix | Delete
Float::INFINITY
[402] Fix | Delete
end
[403] Fix | Delete
end
[404] Fix | Delete
[405] Fix | Delete
# An implementation of +PseudoPrimeGenerator+.
[406] Fix | Delete
#
[407] Fix | Delete
# Uses +EratosthenesSieve+.
[408] Fix | Delete
class EratosthenesGenerator < PseudoPrimeGenerator
[409] Fix | Delete
def initialize
[410] Fix | Delete
@last_prime_index = -1
[411] Fix | Delete
super
[412] Fix | Delete
end
[413] Fix | Delete
[414] Fix | Delete
def succ
[415] Fix | Delete
@last_prime_index += 1
[416] Fix | Delete
EratosthenesSieve.instance.get_nth_prime(@last_prime_index)
[417] Fix | Delete
end
[418] Fix | Delete
def rewind
[419] Fix | Delete
initialize
[420] Fix | Delete
end
[421] Fix | Delete
alias next succ
[422] Fix | Delete
end
[423] Fix | Delete
[424] Fix | Delete
# An implementation of +PseudoPrimeGenerator+ which uses
[425] Fix | Delete
# a prime table generated by trial division.
[426] Fix | Delete
class TrialDivisionGenerator < PseudoPrimeGenerator
[427] Fix | Delete
def initialize
[428] Fix | Delete
@index = -1
[429] Fix | Delete
super
[430] Fix | Delete
end
[431] Fix | Delete
[432] Fix | Delete
def succ
[433] Fix | Delete
TrialDivision.instance[@index += 1]
[434] Fix | Delete
end
[435] Fix | Delete
def rewind
[436] Fix | Delete
initialize
[437] Fix | Delete
end
[438] Fix | Delete
alias next succ
[439] Fix | Delete
end
[440] Fix | Delete
[441] Fix | Delete
# Generates all integers which are greater than 2 and
[442] Fix | Delete
# are not divisible by either 2 or 3.
[443] Fix | Delete
#
[444] Fix | Delete
# This is a pseudo-prime generator, suitable on
[445] Fix | Delete
# checking primality of an integer by brute force
[446] Fix | Delete
# method.
[447] Fix | Delete
class Generator23 < PseudoPrimeGenerator
[448] Fix | Delete
def initialize
[449] Fix | Delete
@prime = 1
[450] Fix | Delete
@step = nil
[451] Fix | Delete
super
[452] Fix | Delete
end
[453] Fix | Delete
[454] Fix | Delete
def succ
[455] Fix | Delete
if (@step)
[456] Fix | Delete
@prime += @step
[457] Fix | Delete
@step = 6 - @step
[458] Fix | Delete
else
[459] Fix | Delete
case @prime
[460] Fix | Delete
when 1; @prime = 2
[461] Fix | Delete
when 2; @prime = 3
[462] Fix | Delete
when 3; @prime = 5; @step = 2
[463] Fix | Delete
end
[464] Fix | Delete
end
[465] Fix | Delete
@prime
[466] Fix | Delete
end
[467] Fix | Delete
alias next succ
[468] Fix | Delete
def rewind
[469] Fix | Delete
initialize
[470] Fix | Delete
end
[471] Fix | Delete
end
[472] Fix | Delete
[473] Fix | Delete
# Internal use. An implementation of prime table by trial division method.
[474] Fix | Delete
class TrialDivision
[475] Fix | Delete
include Singleton
[476] Fix | Delete
[477] Fix | Delete
def initialize # :nodoc:
[478] Fix | Delete
# These are included as class variables to cache them for later uses. If memory
[479] Fix | Delete
# usage is a problem, they can be put in Prime#initialize as instance variables.
[480] Fix | Delete
[481] Fix | Delete
# There must be no primes between @primes[-1] and @next_to_check.
[482] Fix | Delete
@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]
[483] Fix | Delete
# @next_to_check % 6 must be 1.
[484] Fix | Delete
@next_to_check = 103 # @primes[-1] - @primes[-1] % 6 + 7
[485] Fix | Delete
@ulticheck_index = 3 # @primes.index(@primes.reverse.find {|n|
[486] Fix | Delete
# n < Math.sqrt(@@next_to_check) })
[487] Fix | Delete
@ulticheck_next_squared = 121 # @primes[@ulticheck_index + 1] ** 2
[488] Fix | Delete
end
[489] Fix | Delete
[490] Fix | Delete
# Returns the +index+th prime number.
[491] Fix | Delete
#
[492] Fix | Delete
# +index+ is a 0-based index.
[493] Fix | Delete
def [](index)
[494] Fix | Delete
while index >= @primes.length
[495] Fix | Delete
# Only check for prime factors up to the square root of the potential primes,
[496] Fix | Delete
# but without the performance hit of an actual square root calculation.
[497] Fix | Delete
if @next_to_check + 4 > @ulticheck_next_squared
[498] Fix | Delete
@ulticheck_index += 1
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function