mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/prime.rb: Indent examples enough to appear as code sections.
Note that Prime is Enumerable. [#4762] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31880 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
f4273e88a8
commit
b6dd727b86
2 changed files with 37 additions and 18 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Wed Jun 1 09:21:30 2011 Eric Hodel <drbrain@segment7.net>
|
||||||
|
|
||||||
|
* lib/prime.rb: Indent examples enough to appear as code sections.
|
||||||
|
Note that Prime is Enumerable. [#4762]
|
||||||
|
|
||||||
Wed Jun 1 07:34:57 2011 Eric Hodel <drbrain@segment7.net>
|
Wed Jun 1 07:34:57 2011 Eric Hodel <drbrain@segment7.net>
|
||||||
|
|
||||||
* hash.c (key_i): Change rdoc from "the first occurence" to "an
|
* hash.c (key_i): Change rdoc from "the first occurence" to "an
|
||||||
|
|
50
lib/prime.rb
50
lib/prime.rb
|
@ -46,11 +46,17 @@ end
|
||||||
# The set of all prime numbers.
|
# The set of all prime numbers.
|
||||||
#
|
#
|
||||||
# == Example
|
# == Example
|
||||||
# Prime.each(100) do |prime|
|
#
|
||||||
# p prime #=> 2, 3, 5, 7, 11, ...., 97
|
# Prime.each(100) do |prime|
|
||||||
# end
|
# p prime #=> 2, 3, 5, 7, 11, ...., 97
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# Prime is Enumerable:
|
||||||
|
#
|
||||||
|
# Prime.first 5 # => [2, 3, 5, 7, 11]
|
||||||
#
|
#
|
||||||
# == Retrieving the instance
|
# == Retrieving the instance
|
||||||
|
#
|
||||||
# +Prime+.new is obsolete. Now +Prime+ has the default instance and you can
|
# +Prime+.new is obsolete. Now +Prime+ has the default instance and you can
|
||||||
# access it as +Prime+.instance.
|
# access it as +Prime+.instance.
|
||||||
#
|
#
|
||||||
|
@ -58,10 +64,11 @@ end
|
||||||
# as a class method of +Prime+.
|
# as a class method of +Prime+.
|
||||||
#
|
#
|
||||||
# e.g.
|
# e.g.
|
||||||
# Prime.instance.prime?(2) #=> true
|
# Prime.instance.prime?(2) #=> true
|
||||||
# Prime.prime?(2) #=> true
|
# Prime.prime?(2) #=> true
|
||||||
#
|
#
|
||||||
# == Generators
|
# == Generators
|
||||||
|
#
|
||||||
# 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.
|
||||||
# Futhermore, it is a external iterator of prime enumeration which is
|
# Futhermore, it is a external iterator of prime enumeration which is
|
||||||
|
@ -80,6 +87,7 @@ end
|
||||||
# is faster and uses much less memory than other generators. So,
|
# is faster and uses much less memory than 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? .
|
||||||
|
|
||||||
class Prime
|
class Prime
|
||||||
include Enumerable
|
include Enumerable
|
||||||
@the_instance = Prime.new
|
@the_instance = Prime.new
|
||||||
|
@ -105,6 +113,7 @@ class Prime
|
||||||
# Iterates the given block over all prime numbers.
|
# Iterates the given block over all prime numbers.
|
||||||
#
|
#
|
||||||
# == Parameters
|
# == Parameters
|
||||||
|
#
|
||||||
# +ubound+::
|
# +ubound+::
|
||||||
# Optional. An arbitrary positive number.
|
# Optional. An arbitrary positive number.
|
||||||
# The upper bound of enumeration. The method enumerates
|
# The upper bound of enumeration. The method enumerates
|
||||||
|
@ -113,11 +122,13 @@ class Prime
|
||||||
# Optional. An implementation of pseudo-prime generator.
|
# Optional. An implementation of pseudo-prime generator.
|
||||||
#
|
#
|
||||||
# == Return value
|
# == Return value
|
||||||
|
#
|
||||||
# An evaluated value of the given block at the last time.
|
# An evaluated value of the given block at the last time.
|
||||||
# Or an enumerator which is compatible to an +Enumerator+
|
# Or an enumerator which is compatible to an +Enumerator+
|
||||||
# if no block given.
|
# if no block given.
|
||||||
#
|
#
|
||||||
# == Description
|
# == Description
|
||||||
|
#
|
||||||
# Calls +block+ once for each prime number, passing the prime as
|
# Calls +block+ once for each prime number, passing the prime as
|
||||||
# a parameter.
|
# a parameter.
|
||||||
#
|
#
|
||||||
|
@ -126,6 +137,7 @@ class Prime
|
||||||
# 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 a object extended by +Prime+::+OldCompatibility+
|
||||||
# in order to compatibility to Ruby 1.8, and +Prime+#each is overwritten
|
# in order to compatibility to Ruby 1.8, and +Prime+#each is overwritten
|
||||||
# by +Prime+::+OldCompatibility+#+each+.
|
# by +Prime+::+OldCompatibility+#+each+.
|
||||||
|
@ -141,6 +153,7 @@ class Prime
|
||||||
# Returns true if +value+ is prime, false for a composite.
|
# Returns true if +value+ is prime, false for a composite.
|
||||||
#
|
#
|
||||||
# == Parameters
|
# == Parameters
|
||||||
|
#
|
||||||
# +value+:: an arbitrary integer to be checked.
|
# +value+:: an arbitrary integer to be checked.
|
||||||
# +generator+:: optional. A pseudo-prime generator.
|
# +generator+:: optional. A pseudo-prime generator.
|
||||||
def prime?(value, generator = Prime::Generator23.new)
|
def prime?(value, generator = Prime::Generator23.new)
|
||||||
|
@ -161,10 +174,11 @@ class Prime
|
||||||
# and a natural number -- an exponent.
|
# and a natural number -- an exponent.
|
||||||
#
|
#
|
||||||
# == Example
|
# == Example
|
||||||
# For [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]], it returns
|
# For <tt>[[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]]</tt>, it returns:
|
||||||
# p_1**e_1 * p_2**e_2 * .... * p_n**e_n.
|
|
||||||
#
|
#
|
||||||
# Prime.int_from_prime_division([[2,2], [3,1]]) #=> 12
|
# p_1**e_1 * p_2**e_2 * .... * p_n**e_n.
|
||||||
|
#
|
||||||
|
# Prime.int_from_prime_division([[2,2], [3,1]]) #=> 12
|
||||||
def int_from_prime_division(pd)
|
def int_from_prime_division(pd)
|
||||||
pd.inject(1){|value, (prime, index)|
|
pd.inject(1){|value, (prime, index)|
|
||||||
value *= prime**index
|
value *= prime**index
|
||||||
|
@ -185,12 +199,15 @@ class Prime
|
||||||
# +ZeroDivisionError+:: when +value+ is zero.
|
# +ZeroDivisionError+:: when +value+ is zero.
|
||||||
#
|
#
|
||||||
# == Example
|
# == Example
|
||||||
# For an arbitrary integer
|
# For an arbitrary integer:
|
||||||
# n = p_1**e_1 * p_2**e_2 * .... * p_n**e_n,
|
|
||||||
# prime_division(n) returns
|
|
||||||
# [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]].
|
|
||||||
#
|
#
|
||||||
# Prime.prime_division(12) #=> [[2,2], [3,1]]
|
# n = p_1**e_1 * p_2**e_2 * .... * p_n**e_n,
|
||||||
|
#
|
||||||
|
# prime_division(n) returns:
|
||||||
|
#
|
||||||
|
# [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]].
|
||||||
|
#
|
||||||
|
# Prime.prime_division(12) #=> [[2,2], [3,1]]
|
||||||
#
|
#
|
||||||
def prime_division(value, generator= Prime::Generator23.new)
|
def prime_division(value, generator= Prime::Generator23.new)
|
||||||
raise ZeroDivisionError if value == 0
|
raise ZeroDivisionError if value == 0
|
||||||
|
@ -353,9 +370,6 @@ class Prime
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Internal use. An implementation of prime table by trial division method.
|
# Internal use. An implementation of prime table by trial division method.
|
||||||
class TrialDivision
|
class TrialDivision
|
||||||
include Singleton
|
include Singleton
|
||||||
|
@ -483,8 +497,8 @@ class Prime
|
||||||
|
|
||||||
# Overwrites Prime#each.
|
# Overwrites Prime#each.
|
||||||
#
|
#
|
||||||
# Iterates the given block over all prime numbers. Note that enumeration starts from
|
# Iterates the given block over all prime numbers. Note that enumeration
|
||||||
# the current position of internal pointer, not rewound.
|
# starts from the current position of internal pointer, not rewound.
|
||||||
def each(&block)
|
def each(&block)
|
||||||
return @generator.dup unless block_given?
|
return @generator.dup unless block_given?
|
||||||
loop do
|
loop do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue