summaryrefslogtreecommitdiff
path: root/lib/prime.rb
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-06 03:56:38 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-06 03:56:38 +0000
commit287a34ae0dfc23e4158f67cb7783d239f202c368 (patch)
tree5e35d5b41aae961b37cf6632f60c42f51c7aa775 /lib/prime.rb
parent9b52ae2e6491bb5d6c59e1799449f6268baf6f89 (diff)
* {ext,lib,test}/**/*.rb: removed trailing spaces.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22784 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/prime.rb')
-rw-r--r--lib/prime.rb56
1 files changed, 28 insertions, 28 deletions
diff --git a/lib/prime.rb b/lib/prime.rb
index ce71d5e00f..ec3f8fa8cb 100644
--- a/lib/prime.rb
+++ b/lib/prime.rb
@@ -21,9 +21,9 @@ class Integer
def Integer.from_prime_division(pd)
Prime.int_from_prime_division(pd)
end
-
+
# Returns the factorization of +self+.
- #
+ #
# See Prime#prime_division for more details.
def prime_division(generator = Prime::Generator23.new)
Prime.prime_division(self, generator)
@@ -34,7 +34,7 @@ class Integer
Prime.prime?(self)
end
- # Iterates the given block over all prime numbers.
+ # Iterates the given block over all prime numbers.
#
# See +Prime+#each for more details.
def Integer.each_prime(ubound, &block) # :yields: prime
@@ -51,11 +51,11 @@ end
# end
#
# == 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.
#
# For convenience, each instance method of +Prime+.instance can be accessed
-# as a class method of +Prime+.
+# as a class method of +Prime+.
#
# e.g.
# Prime.instance.prime?(2) #=> true
@@ -64,19 +64,19 @@ end
# == 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
+# Futhermore, it is a external iterator of prime enumeration which is
# compatible to an Enumerator.
#
# +Prime+::+PseudoPrimeGenerator+ is the base class for generators.
# There are few implementations of generator.
#
# [+Prime+::+EratosthenesGenerator+]
-# Uses eratosthenes's sieve.
+# Uses eratosthenes's sieve.
# [+Prime+::+TrialDivisionGenerator+]
# Uses the trial division method.
# [+Prime+::+Generator23+]
# Generates all positive integers which is not divided by 2 nor 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,
# it is suitable for factorizing an integer which is not large but
# has many prime factors. e.g. for Prime#prime? .
@@ -106,23 +106,23 @@ class Prime
#
# == Parameters
# +ubound+::
- # Optional. An arbitrary positive number.
+ # Optional. An arbitrary positive number.
# The upper bound of enumeration. The method enumerates
- # prime numbers infinitely if +ubound+ is nil.
+ # prime numbers infinitely if +ubound+ is nil.
# +generator+::
# 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.
+ # if no block given.
#
# == Description
# Calls +block+ once for each prime numer, passing the prime as
# a parameter.
#
# +ubound+::
- # Upper bound of prime numbers. The iterator stops after
+ # Upper bound of prime numbers. The iterator stops after
# yields all prime numbers p <= +ubound+.
#
# == Note
@@ -156,9 +156,9 @@ class Prime
# Re-composes a prime factorization and returns the product.
#
# == Parameters
- # +pd+:: Array of pairs of integers. The each internal
+ # +pd+:: Array of pairs of integers. The each internal
# pair consists of a prime number -- a prime factor --
- # and a natural number -- an exponent.
+ # and a natural number -- an exponent.
#
# == Example
# For [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]], it returns
@@ -176,7 +176,7 @@ class Prime
# == Parameters
# +value+:: An arbitrary integer.
# +generator+:: Optional. A pseudo-prime generator.
- # +generator+.succ must return the next
+ # +generator+.succ must return the next
# pseudo-prime number in the ascendent
# order. It must generate all prime numbers,
# but may generate non prime numbers.
@@ -185,7 +185,7 @@ class Prime
# +ZeroDivisionError+:: when +value+ is zero.
#
# == 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]].
@@ -231,9 +231,9 @@ class Prime
end
# returns the next pseudo-prime number, and move the internal
- # position forward.
+ # position forward.
#
- # +PseudoPrimeGenerator+#succ raises +NotImplementedError+.
+ # +PseudoPrimeGenerator+#succ raises +NotImplementedError+.
def succ
raise NotImplementedError, "need to define `succ'"
end
@@ -278,7 +278,7 @@ class Prime
end
end
end
-
+
# An implementation of +PseudoPrimeGenerator+.
#
# Uses +EratosthenesSieve+.
@@ -286,7 +286,7 @@ class Prime
def initialize
@last_prime = nil
end
-
+
def succ
@last_prime = @last_prime ? EratosthenesSieve.instance.next_to(@last_prime) : 2
end
@@ -296,13 +296,13 @@ class Prime
alias next succ
end
- # An implementation of +PseudoPrimeGenerator+ which uses
+ # An implementation of +PseudoPrimeGenerator+ which uses
# a prime table generated by trial division.
class TrialDivisionGenerator<PseudoPrimeGenerator
def initialize
@index = -1
end
-
+
def succ
TrialDivision.instance[@index += 1]
end
@@ -315,15 +315,15 @@ class Prime
# Generates all integer which are greater than 2 and
# are not divided by 2 nor 3.
#
- # This is a pseudo-prime generator, suitable on
- # checking primality of a integer by brute force
+ # This is a pseudo-prime generator, suitable on
+ # checking primality of a integer by brute force
# method.
class Generator23<PseudoPrimeGenerator
def initialize
@prime = 1
@step = nil
end
-
+
def succ
loop do
if (@step)
@@ -358,7 +358,7 @@ class Prime
# There must be no primes between @primes[-1] and @next_to_check.
@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]
- # @next_to_check % 6 must be 1.
+ # @next_to_check % 6 must be 1.
@next_to_check = 103 # @primes[-1] - @primes[-1] % 6 + 7
@ulticheck_index = 3 # @primes.index(@primes.reverse.find {|n|
# n < Math.sqrt(@@next_to_check) })
@@ -372,7 +372,7 @@ class Prime
alias primes cache
alias primes_so_far cache
- # Returns the +index+th prime number.
+ # Returns the +index+th prime number.
#
# +index+ is a 0-based index.
def [](index)
@@ -390,7 +390,7 @@ class Prime
@primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?
@next_to_check += 4
@primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?
- @next_to_check += 2
+ @next_to_check += 2
end
return @primes[index]
end