mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/prime.rb: Corrected a few comments. Patch by @Nullset14.
Fixes GH-346. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41714 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
305f06c7e3
commit
3628ab8092
2 changed files with 23 additions and 18 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Sun Jun 30 22:40:00 2013 Charlie Somerville <charliesome@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/prime.rb: Corrected a few comments. Patch by @Nullset14.
|
||||||
|
Fixes GH-346.
|
||||||
|
|
||||||
Sun Jun 30 21:53:38 2013 Tanaka Akira <akr@fsij.org>
|
Sun Jun 30 21:53:38 2013 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* bignum.c (rb_cstr_to_inum): Use rb_integer_unpack if base is a power
|
* bignum.c (rb_cstr_to_inum): Use rb_integer_unpack if base is a power
|
||||||
|
|
34
lib/prime.rb
34
lib/prime.rb
|
@ -71,20 +71,20 @@ end
|
||||||
#
|
#
|
||||||
# A "generator" provides an implementation of enumerating pseudo-prime
|
# A "generator" provides an implementation of enumerating pseudo-prime
|
||||||
# numbers and it remembers the position of enumeration and upper bound.
|
# numbers and it remembers the position of enumeration and upper bound.
|
||||||
# Furthermore, it is a external iterator of prime enumeration which is
|
# Furthermore, it is an external iterator of prime enumeration which is
|
||||||
# compatible to an Enumerator.
|
# compatible with an Enumerator.
|
||||||
#
|
#
|
||||||
# +Prime+::+PseudoPrimeGenerator+ is the base class for generators.
|
# +Prime+::+PseudoPrimeGenerator+ is the base class for generators.
|
||||||
# There are few implementations of generator.
|
# There are few implementations of generator.
|
||||||
#
|
#
|
||||||
# [+Prime+::+EratosthenesGenerator+]
|
# [+Prime+::+EratosthenesGenerator+]
|
||||||
# Uses eratosthenes's sieve.
|
# Uses eratosthenes' sieve.
|
||||||
# [+Prime+::+TrialDivisionGenerator+]
|
# [+Prime+::+TrialDivisionGenerator+]
|
||||||
# Uses the trial division method.
|
# Uses the trial division method.
|
||||||
# [+Prime+::+Generator23+]
|
# [+Prime+::+Generator23+]
|
||||||
# Generates all positive integers which is not divided by 2 nor 3.
|
# Generates all positive integers which are not divisible by either 2 or 3.
|
||||||
# This sequence is very bad as a pseudo-prime sequence. But this
|
# This sequence is very bad as a pseudo-prime sequence. But this
|
||||||
# is faster and uses much less memory than other generators. So,
|
# is faster and uses much less memory than the other generators. So,
|
||||||
# it is suitable for factorizing an integer which is not large but
|
# it is suitable for factorizing an integer which is not large but
|
||||||
# has many prime factors. e.g. for Prime#prime? .
|
# has many prime factors. e.g. for Prime#prime? .
|
||||||
|
|
||||||
|
@ -133,13 +133,13 @@ class Prime
|
||||||
# a parameter.
|
# a parameter.
|
||||||
#
|
#
|
||||||
# +ubound+::
|
# +ubound+::
|
||||||
# Upper bound of prime numbers. The iterator stops after
|
# Upper bound of prime numbers. The iterator stops after it
|
||||||
# yields all prime numbers p <= +ubound+.
|
# yields all prime numbers p <= +ubound+.
|
||||||
#
|
#
|
||||||
# == Note
|
# == Note
|
||||||
#
|
#
|
||||||
# +Prime+.+new+ returns a object extended by +Prime+::+OldCompatibility+
|
# +Prime+.+new+ returns an object extended by +Prime+::+OldCompatibility+
|
||||||
# in order to compatibility to Ruby 1.8, and +Prime+#each is overwritten
|
# in order to be compatible with Ruby 1.8, and +Prime+#each is overwritten
|
||||||
# by +Prime+::+OldCompatibility+#+each+.
|
# by +Prime+::+OldCompatibility+#+each+.
|
||||||
#
|
#
|
||||||
# +Prime+.+new+ is now obsolete. Use +Prime+.+instance+.+each+ or simply
|
# +Prime+.+new+ is now obsolete. Use +Prime+.+instance+.+each+ or simply
|
||||||
|
@ -191,9 +191,9 @@ class Prime
|
||||||
# +value+:: An arbitrary integer.
|
# +value+:: An arbitrary integer.
|
||||||
# +generator+:: Optional. A pseudo-prime generator.
|
# +generator+:: Optional. A pseudo-prime generator.
|
||||||
# +generator+.succ must return the next
|
# +generator+.succ must return the next
|
||||||
# pseudo-prime number in the ascendent
|
# pseudo-prime number in the ascending
|
||||||
# order. It must generate all prime numbers,
|
# order. It must generate all prime numbers,
|
||||||
# but may generate non prime numbers.
|
# but may also generate non prime numbers too.
|
||||||
#
|
#
|
||||||
# === Exceptions
|
# === Exceptions
|
||||||
# +ZeroDivisionError+:: when +value+ is zero.
|
# +ZeroDivisionError+:: when +value+ is zero.
|
||||||
|
@ -272,7 +272,7 @@ class Prime
|
||||||
raise NotImplementedError, "need to define `rewind'"
|
raise NotImplementedError, "need to define `rewind'"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Iterates the given block for each prime numbers.
|
# Iterates the given block for each prime number.
|
||||||
def each(&block)
|
def each(&block)
|
||||||
return self.dup unless block
|
return self.dup unless block
|
||||||
if @ubound
|
if @ubound
|
||||||
|
@ -336,11 +336,11 @@ class Prime
|
||||||
alias next succ
|
alias next succ
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generates all integer which are greater than 2 and
|
# Generates all integers which are greater than 2 and
|
||||||
# are not divided by 2 nor 3.
|
# are not divisible by either 2 or 3.
|
||||||
#
|
#
|
||||||
# This is a pseudo-prime generator, suitable on
|
# This is a pseudo-prime generator, suitable on
|
||||||
# checking primality of a integer by brute force
|
# checking primality of an integer by brute force
|
||||||
# method.
|
# method.
|
||||||
class Generator23<PseudoPrimeGenerator
|
class Generator23<PseudoPrimeGenerator
|
||||||
def initialize
|
def initialize
|
||||||
|
@ -418,7 +418,7 @@ class Prime
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Internal use. An implementation of eratosthenes's sieve
|
# Internal use. An implementation of eratosthenes' sieve
|
||||||
class EratosthenesSieve
|
class EratosthenesSieve
|
||||||
include Singleton
|
include Singleton
|
||||||
|
|
||||||
|
@ -456,7 +456,7 @@ class Prime
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
# for an odd number +n+, returns (i, j, k) such that @tables[i][j][k] represents primarity of the number
|
# for an odd number +n+, returns (i, j, k) such that @tables[i][j][k] represents primality of the number
|
||||||
def indices(n)
|
def indices(n)
|
||||||
# binary digits of n: |0|1|2|3|4|5|6|7|8|9|10|11|....
|
# binary digits of n: |0|1|2|3|4|5|6|7|8|9|10|11|....
|
||||||
# indices: |-| k | j | i
|
# indices: |-| k | j | i
|
||||||
|
@ -471,7 +471,7 @@ class Prime
|
||||||
def extend_table
|
def extend_table
|
||||||
lbound = NUMS_PER_TABLE * @tables.length
|
lbound = NUMS_PER_TABLE * @tables.length
|
||||||
ubound = lbound + NUMS_PER_TABLE
|
ubound = lbound + NUMS_PER_TABLE
|
||||||
new_table = [FILLED_ENTRY] * ENTRIES_PER_TABLE # which represents primarity in lbound...ubound
|
new_table = [FILLED_ENTRY] * ENTRIES_PER_TABLE # which represents primality in lbound...ubound
|
||||||
(3..Integer(Math.sqrt(ubound))).step(2) do |p|
|
(3..Integer(Math.sqrt(ubound))).step(2) do |p|
|
||||||
i, j, k = indices(p)
|
i, j, k = indices(p)
|
||||||
next if @tables[i][j][k].zero?
|
next if @tables[i][j][k].zero?
|
||||||
|
|
Loading…
Add table
Reference in a new issue