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