Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* 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
  • Loading branch information...
commit b6dd727b86a9107dfb6f00164151fc59d50e8605 1 parent f4273e8
@drbrain drbrain authored
Showing with 37 additions and 18 deletions.
  1. +5 −0 ChangeLog
  2. +32 −18 lib/prime.rb
View
5 ChangeLog
@@ -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>
* hash.c (key_i): Change rdoc from "the first occurence" to "an
View
50 lib/prime.rb
@@ -46,11 +46,17 @@ def Integer.each_prime(ubound, &block) # :yields: prime
# The set of all prime numbers.
#
# == Example
-# Prime.each(100) do |prime|
-# p prime #=> 2, 3, 5, 7, 11, ...., 97
-# end
+#
+# Prime.each(100) do |prime|
+# p prime #=> 2, 3, 5, 7, 11, ...., 97
+# end
+#
+# Prime is Enumerable:
+#
+# Prime.first 5 # => [2, 3, 5, 7, 11]
#
# == Retrieving the instance
+#
# +Prime+.new is obsolete. Now +Prime+ has the default instance and you can
# access it as +Prime+.instance.
#
@@ -58,10 +64,11 @@ def Integer.each_prime(ubound, &block) # :yields: prime
# as a class method of +Prime+.
#
# e.g.
-# Prime.instance.prime?(2) #=> true
-# Prime.prime?(2) #=> true
+# Prime.instance.prime?(2) #=> true
+# Prime.prime?(2) #=> true
#
# == Generators
+#
# A "generator" provides an implementation of enumerating pseudo-prime
# numbers and it remembers the position of enumeration and upper bound.
# Futhermore, it is a external iterator of prime enumeration which is
@@ -80,6 +87,7 @@ def Integer.each_prime(ubound, &block) # :yields: prime
# is faster and uses much less memory than other generators. So,
# it is suitable for factorizing an integer which is not large but
# has many prime factors. e.g. for Prime#prime? .
+
class Prime
include Enumerable
@the_instance = Prime.new
@@ -105,6 +113,7 @@ def method_added(method) # :nodoc:
# Iterates the given block over all prime numbers.
#
# == Parameters
+ #
# +ubound+::
# Optional. An arbitrary positive number.
# The upper bound of enumeration. The method enumerates
@@ -113,11 +122,13 @@ def method_added(method) # :nodoc:
# Optional. An implementation of pseudo-prime generator.
#
# == Return value
+ #
# An evaluated value of the given block at the last time.
# Or an enumerator which is compatible to an +Enumerator+
# if no block given.
#
# == Description
+ #
# Calls +block+ once for each prime number, passing the prime as
# a parameter.
#
@@ -126,6 +137,7 @@ def method_added(method) # :nodoc:
# yields all prime numbers p <= +ubound+.
#
# == Note
+ #
# +Prime+.+new+ returns a object extended by +Prime+::+OldCompatibility+
# in order to compatibility to Ruby 1.8, and +Prime+#each is overwritten
# by +Prime+::+OldCompatibility+#+each+.
@@ -141,6 +153,7 @@ def each(ubound = nil, generator = EratosthenesGenerator.new, &block)
# Returns true if +value+ is prime, false for a composite.
#
# == Parameters
+ #
# +value+:: an arbitrary integer to be checked.
# +generator+:: optional. A pseudo-prime generator.
def prime?(value, generator = Prime::Generator23.new)
@@ -161,10 +174,11 @@ def prime?(value, generator = Prime::Generator23.new)
# and a natural number -- an exponent.
#
# == Example
- # For [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]], it returns
- # p_1**e_1 * p_2**e_2 * .... * p_n**e_n.
+ # 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
+ # Prime.int_from_prime_division([[2,2], [3,1]]) #=> 12
def int_from_prime_division(pd)
pd.inject(1){|value, (prime, index)|
value *= prime**index
@@ -185,12 +199,15 @@ def int_from_prime_division(pd)
# +ZeroDivisionError+:: when +value+ is zero.
#
# == Example
- # 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]].
+ # For an arbitrary integer:
+ #
+ # n = p_1**e_1 * p_2**e_2 * .... * p_n**e_n,
+ #
+ # prime_division(n) returns:
#
- # Prime.prime_division(12) #=> [[2,2], [3,1]]
+ # [[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)
raise ZeroDivisionError if value == 0
@@ -353,9 +370,6 @@ def rewind
end
end
-
-
-
# Internal use. An implementation of prime table by trial division method.
class TrialDivision
include Singleton
@@ -483,8 +497,8 @@ def succ
# Overwrites Prime#each.
#
- # Iterates the given block over all prime numbers. Note that enumeration starts from
- # the current position of internal pointer, not rewound.
+ # Iterates the given block over all prime numbers. Note that enumeration
+ # starts from the current position of internal pointer, not rewound.
def each(&block)
return @generator.dup unless block_given?
loop do
Please sign in to comment.
Something went wrong with that request. Please try again.