https://bugs.ruby-lang.org/
https://bugs.ruby-lang.org/favicon.ico?1711330511
2010-09-10T16:32:17Z
Ruby Issue Tracking System
Ruby master - Feature #3814: Prime.prime?をC拡張にする
https://bugs.ruby-lang.org/issues/3814?journal_id=13251
2010-09-10T16:32:17Z
sorah (Sorah Fukumori)
her@sorah.jp
<ul></ul><p>=begin<br>
パッチのおかしなところを修正しました。</p>
<p>diff --git ext/prime/extconf.rb ext/prime/extconf.rb<br>
new file mode 100644<br>
index 0000000..c7e6941<br>
--- /dev/null<br>
+++ ext/prime/extconf.rb<br>
@@ -0,0 +1,3 @@<br>
+require 'mkmf'<br>
+<br>
+create_makefile('prime')<br>
diff --git ext/prime/lib/prime.rb ext/prime/lib/prime.rb<br>
new file mode 100644<br>
index 0000000..741164a<br>
--- /dev/null<br>
+++ ext/prime/lib/prime.rb<br>
@@ -0,0 +1,499 @@<br>
+#<br>
+# = prime.rb<br>
+#<br>
+# Prime numbers and factorization library.<br>
+#<br>
+# Copyright::<br>
+# Copyright (c) 1998-2008 Keiju ISHITSUKA(SHL Japan Inc.)<br>
+# Copyright (c) 2008 Yuki Sonoda (Yugui) <a href="mailto:yugui@yugui.jp" class="email">yugui@yugui.jp</a><br>
+#<br>
+# Documentation::<br>
+# Yuki Sonoda<br>
+#<br>
+<br>
+require "singleton"<br>
+require "forwardable"<br>
+<br>
+class Integer</p>
<ul>
<li>
<a name="Re-composes-a-prime-factorization-and-returns-the-product"></a>
<h1 >Re-composes a prime factorization and returns the product.<a href="#Re-composes-a-prime-factorization-and-returns-the-product" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="See-Primeint_from_prime_division-for-more-details"></a>
<h1 >See Prime#int_from_prime_division for more details.<a href="#See-Primeint_from_prime_division-for-more-details" class="wiki-anchor">¶</a></h1>
</li>
<li>def Integer.from_prime_division(pd)</li>
<li>Prime.int_from_prime_division(pd)</li>
<li>end</li>
<li>
<li>
<a name="Returns-the-factorization-of-self"></a>
<h1 >Returns the factorization of +self+.<a href="#Returns-the-factorization-of-self" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="See-Primeprime_division-for-more-details"></a>
<h1 >See Prime#prime_division for more details.<a href="#See-Primeprime_division-for-more-details" class="wiki-anchor">¶</a></h1>
</li>
<li>def prime_division(generator = Prime::Generator23.new)</li>
<li>Prime.prime_division(self, generator)</li>
<li>end</li>
<li>
<li>
<a name="Returns-true-if-self-is-a-prime-number-false-for-a-composite"></a>
<h1 >Returns true if +self+ is a prime number, false for a composite.<a href="#Returns-true-if-self-is-a-prime-number-false-for-a-composite" class="wiki-anchor">¶</a></h1>
</li>
<li>def prime?</li>
<li>Prime.prime?(self)</li>
<li>end</li>
<li>
<li>
<a name="Iterates-the-given-block-over-all-prime-numbers"></a>
<h1 >Iterates the given block over all prime numbers.<a href="#Iterates-the-given-block-over-all-prime-numbers" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="See-Primeeach-for-more-details"></a>
<h1 >See +Prime+#each for more details.<a href="#See-Primeeach-for-more-details" class="wiki-anchor">¶</a></h1>
</li>
<li>def Integer.each_prime(ubound, &block) # :yields: prime</li>
<li>Prime.each(ubound, &block)</li>
<li>end<br>
+end</li>
<li>
</ul>
<p>+#<br>
+# The set of all prime numbers.<br>
+#<br>
+# == Example<br>
+# Prime.each(100) do |prime|<br>
+# p prime #=> 2, 3, 5, 7, 11, ...., 97<br>
+# end<br>
+#<br>
+# == Retrieving the instance<br>
+# +Prime+.new is obsolete. Now +Prime+ has the default instance and you can<br>
+# access it as +Prime+.instance.<br>
+#<br>
+# For convenience, each instance method of +Prime+.instance can be accessed<br>
+# as a class method of +Prime+.<br>
+#<br>
+# e.g.<br>
+# Prime.instance.prime?(2) #=> true<br>
+# Prime.prime?(2) #=> true<br>
+#<br>
+# == Generators<br>
+# A "generator" provides an implementation of enumerating pseudo-prime<br>
+# numbers and it remembers the position of enumeration and upper bound.<br>
+# Futhermore, it is a external iterator of prime enumeration which is<br>
+# compatible to an Enumerator.<br>
+#<br>
+# +Prime+::+PseudoPrimeGenerator+ is the base class for generators.<br>
+# There are few implementations of generator.<br>
+#<br>
+# [+Prime+::+EratosthenesGenerator+]<br>
+# Uses eratosthenes's sieve.<br>
+# [+Prime+::+TrialDivisionGenerator+]<br>
+# Uses the trial division method.<br>
+# [+Prime+::+Generator23+]<br>
+# Generates all positive integers which is not divided by 2 nor 3.<br>
+# This sequence is very bad as a pseudo-prime sequence. But this<br>
+# is faster and uses much less memory than other generators. So,<br>
+# it is suitable for factorizing an integer which is not large but<br>
+# has many prime factors. e.g. for Prime#prime? .<br>
+class Prime</p>
<ul>
<li>include Enumerable</li>
<li>@the_instance = Prime.new</li>
<li>
<li>
<a name="obsolete-Use-Primeinstance-or-class-methods-of-Prime"></a>
<h1 >obsolete. Use +Prime+::+instance+ or class methods of +Prime+.<a href="#obsolete-Use-Primeinstance-or-class-methods-of-Prime" class="wiki-anchor">¶</a></h1>
</li>
<li>def initialize</li>
<li>@generator = EratosthenesGenerator.new</li>
<li>extend OldCompatibility</li>
<li>warn "Prime::new is obsolete. use Prime::instance or class methods of Prime."</li>
<li>end</li>
<li>
<li>class << self</li>
<li>extend Forwardable</li>
<li>include Enumerable</li>
<li>
<a name="Returns-the-default-instance-of-Prime"></a>
<h1 >Returns the default instance of Prime.<a href="#Returns-the-default-instance-of-Prime" class="wiki-anchor">¶</a></h1>
</li>
<li>def instance; @the_instance end</li>
<li>
<li>def method_added(method) # :nodoc:</li>
<li>
<pre><code> (class<< self;self;end).def_delegator :instance, method
</code></pre>
</li>
<li>end</li>
<li>end</li>
<li>
<li>
<a name="Iterates-the-given-block-over-all-prime-numbers-2"></a>
<h1 >Iterates the given block over all prime numbers.<a href="#Iterates-the-given-block-over-all-prime-numbers-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Parameters"></a>
<h1 >== Parameters<a href="#-Parameters" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="ubound"></a>
<h1 >+ubound+::<a href="#ubound" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Optional-An-arbitrary-positive-number"></a>
<h1 >Optional. An arbitrary positive number.<a href="#Optional-An-arbitrary-positive-number" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="The-upper-bound-of-enumeration-The-method-enumerates"></a>
<h1 >The upper bound of enumeration. The method enumerates<a href="#The-upper-bound-of-enumeration-The-method-enumerates" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="prime-numbers-infinitely-if-ubound-is-nil"></a>
<h1 >prime numbers infinitely if +ubound+ is nil.<a href="#prime-numbers-infinitely-if-ubound-is-nil" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="generator"></a>
<h1 >+generator+::<a href="#generator" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Optional-An-implementation-of-pseudo-prime-generator"></a>
<h1 >Optional. An implementation of pseudo-prime generator.<a href="#Optional-An-implementation-of-pseudo-prime-generator" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Return-value"></a>
<h1 >== Return value<a href="#-Return-value" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="An-evaluated-value-of-the-given-block-at-the-last-time"></a>
<h1 >An evaluated value of the given block at the last time.<a href="#An-evaluated-value-of-the-given-block-at-the-last-time" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Or-an-enumerator-which-is-compatible-to-an-Enumerator"></a>
<h1 >Or an enumerator which is compatible to an +Enumerator+<a href="#Or-an-enumerator-which-is-compatible-to-an-Enumerator" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="if-no-block-given"></a>
<h1 >if no block given.<a href="#if-no-block-given" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Description"></a>
<h1 >== Description<a href="#-Description" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Calls-block-once-for-each-prime-number-passing-the-prime-as"></a>
<h1 >Calls +block+ once for each prime number, passing the prime as<a href="#Calls-block-once-for-each-prime-number-passing-the-prime-as" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="a-parameter"></a>
<h1 >a parameter.<a href="#a-parameter" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="ubound-2"></a>
<h1 >+ubound+::<a href="#ubound-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Upper-bound-of-prime-numbers-The-iterator-stops-after"></a>
<h1 >Upper bound of prime numbers. The iterator stops after<a href="#Upper-bound-of-prime-numbers-The-iterator-stops-after" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="yields-all-prime-numbers-p-lt-ubound"></a>
<h1 >yields all prime numbers p <= +ubound+.<a href="#yields-all-prime-numbers-p-lt-ubound" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Note"></a>
<h1 >== Note<a href="#-Note" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Primenew-returns-a-object-extended-by-PrimeOldCompatibility"></a>
<h1 >+Prime+.+new+ returns a object extended by +Prime+::+OldCompatibility+<a href="#Primenew-returns-a-object-extended-by-PrimeOldCompatibility" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="in-order-to-compatibility-to-Ruby-18-and-Primeeach-is-overwritten"></a>
<h1 >in order to compatibility to Ruby 1.8, and +Prime+#each is overwritten<a href="#in-order-to-compatibility-to-Ruby-18-and-Primeeach-is-overwritten" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="by-PrimeOldCompatibilityeach"></a>
<h1 >by +Prime+::+OldCompatibility+#+each+.<a href="#by-PrimeOldCompatibilityeach" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="Primenew-is-now-obsolete-Use-Primeinstanceeach-or-simply"></a>
<h1 >+Prime+.+new+ is now obsolete. Use +Prime+.+instance+.+each+ or simply<a href="#Primenew-is-now-obsolete-Use-Primeinstanceeach-or-simply" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Primeeach"></a>
<h1 >+Prime+.+each+.<a href="#Primeeach" class="wiki-anchor">¶</a></h1>
</li>
<li>def each(ubound = nil, generator = EratosthenesGenerator.new, &block)</li>
<li>generator.upper_bound = ubound</li>
<li>generator.each(&block)</li>
<li>end</li>
<li>
<li>
<li>
<a name="Returns-true-if-value-is-prime-false-for-a-composite"></a>
<h1 >Returns true if +value+ is prime, false for a composite.<a href="#Returns-true-if-value-is-prime-false-for-a-composite" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Parameters-2"></a>
<h1 >== Parameters<a href="#-Parameters-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="value-an-arbitrary-integer-to-be-checked"></a>
<h1 >+value+:: an arbitrary integer to be checked.<a href="#value-an-arbitrary-integer-to-be-checked" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="generator-optional-A-pseudo-prime-generator"></a>
<h1 >+generator+:: optional. A pseudo-prime generator.<a href="#generator-optional-A-pseudo-prime-generator" class="wiki-anchor">¶</a></h1>
</li>
<li>def prime?(value, generator = Prime::Generator23.new)</li>
<li>value = -value if value < 0</li>
<li>return false if value < 2</li>
<li>for num in generator</li>
<li>
<pre><code> q,r = value.divmod num
</code></pre>
</li>
<li>
<pre><code> return true if q < num
</code></pre>
</li>
<li>
<pre><code> return false if r == 0
</code></pre>
</li>
<li>end</li>
<li>end</li>
<li>
<li>
<a name="Re-composes-a-prime-factorization-and-returns-the-product-2"></a>
<h1 >Re-composes a prime factorization and returns the product.<a href="#Re-composes-a-prime-factorization-and-returns-the-product-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Parameters-3"></a>
<h1 >== Parameters<a href="#-Parameters-3" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="pd-Array-of-pairs-of-integers-The-each-internal"></a>
<h1 >+pd+:: Array of pairs of integers. The each internal<a href="#pd-Array-of-pairs-of-integers-The-each-internal" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="pair-consists-of-a-prime-number-a-prime-factor-"></a>
<h1 >pair consists of a prime number -- a prime factor --<a href="#pair-consists-of-a-prime-number-a-prime-factor-" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="and-a-natural-number-an-exponent"></a>
<h1 >and a natural number -- an exponent.<a href="#and-a-natural-number-an-exponent" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Example"></a>
<h1 >== Example<a href="#-Example" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="For-p_1-e_1-p_2-e_2-p_n-e_n-it-returns"></a>
<h1 >For [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]], it returns<a href="#For-p_1-e_1-p_2-e_2-p_n-e_n-it-returns" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="p_1e_1-p_2e_2-p_ne_n"></a>
<h1 >p_1<strong>e_1 * p_2</strong>e_2 * .... * p_n**e_n.<a href="#p_1e_1-p_2e_2-p_ne_n" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="Primeint_from_prime_division22-31-gt-12"></a>
<h1 >Prime.int_from_prime_division([[2,2], [3,1]]) #=> 12<a href="#Primeint_from_prime_division22-31-gt-12" class="wiki-anchor">¶</a></h1>
</li>
<li>def int_from_prime_division(pd)</li>
<li>pd.inject(1){|value, (prime, index)|</li>
<li>
<pre><code> value *= prime**index
</code></pre>
</li>
<li>}</li>
<li>end</li>
<li>
<li>
<a name="Returns-the-factorization-of-value"></a>
<h1 >Returns the factorization of +value+.<a href="#Returns-the-factorization-of-value" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Parameters-4"></a>
<h1 >== Parameters<a href="#-Parameters-4" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="value-An-arbitrary-integer"></a>
<h1 >+value+:: An arbitrary integer.<a href="#value-An-arbitrary-integer" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="generator-Optional-A-pseudo-prime-generator"></a>
<h1 >+generator+:: Optional. A pseudo-prime generator.<a href="#generator-Optional-A-pseudo-prime-generator" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="generatorsucc-must-return-the-next"></a>
<h1 >+generator+.succ must return the next<a href="#generatorsucc-must-return-the-next" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="pseudo-prime-number-in-the-ascendent"></a>
<h1 >pseudo-prime number in the ascendent<a href="#pseudo-prime-number-in-the-ascendent" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="order-It-must-generate-all-prime-numbers"></a>
<h1 >order. It must generate all prime numbers,<a href="#order-It-must-generate-all-prime-numbers" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="but-may-generate-non-prime-numbers"></a>
<h1 >but may generate non prime numbers.<a href="#but-may-generate-non-prime-numbers" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Exceptions"></a>
<h1 >=== Exceptions<a href="#-Exceptions" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="ZeroDivisionError-when-value-is-zero"></a>
<h1 >+ZeroDivisionError+:: when +value+ is zero.<a href="#ZeroDivisionError-when-value-is-zero" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Example-2"></a>
<h1 >== Example<a href="#-Example-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="For-an-arbitrary-integer"></a>
<h1 >For an arbitrary integer<a href="#For-an-arbitrary-integer" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="n-p_1e_1-p_2e_2-p_ne_n"></a>
<h1 >n = p_1<strong>e_1 * p_2</strong>e_2 * .... * p_n**e_n,<a href="#n-p_1e_1-p_2e_2-p_ne_n" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="prime_divisionn-returns"></a>
<h1 >prime_division(n) returns<a href="#prime_divisionn-returns" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="p_1-e_1-p_2-e_2-p_n-e_n"></a>
<h1 >[[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]].<a href="#p_1-e_1-p_2-e_2-p_n-e_n" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="Primeprime_division12-gt-22-31"></a>
<h1 >Prime.prime_division(12) #=> [[2,2], [3,1]]<a href="#Primeprime_division12-gt-22-31" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>def prime_division(value, generator= Prime::Generator23.new)</li>
<li>raise ZeroDivisionError if value == 0</li>
<li>if value < 0</li>
<li>
<pre><code> value = -value
</code></pre>
</li>
<li>
<pre><code> pv = [[-1, 1]]
</code></pre>
</li>
<li>else</li>
<li>
<pre><code> pv = []
</code></pre>
</li>
<li>end</li>
<li>for prime in generator</li>
<li>
<pre><code> count = 0
</code></pre>
</li>
<li>
<pre><code> while (value1, mod = value.divmod(prime)
</code></pre>
</li>
<li>
<pre><code> mod) == 0
</code></pre>
</li>
<li>value = value1</li>
<li>count += 1</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>
<pre><code> if count != 0
</code></pre>
</li>
<li>pv.push [prime, count]</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>
<pre><code> break if value1 <= prime
</code></pre>
</li>
<li>end</li>
<li>if value > 1</li>
<li>
<pre><code> pv.push [value, 1]
</code></pre>
</li>
<li>end</li>
<li>return pv</li>
<li>end</li>
<li>
<li>
<a name="An-abstract-class-for-enumerating-pseudo-prime-numbers"></a>
<h1 >An abstract class for enumerating pseudo-prime numbers.<a href="#An-abstract-class-for-enumerating-pseudo-prime-numbers" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="Concrete-subclasses-should-override-succ-next-rewind"></a>
<h1 >Concrete subclasses should override succ, next, rewind.<a href="#Concrete-subclasses-should-override-succ-next-rewind" class="wiki-anchor">¶</a></h1>
</li>
<li>class PseudoPrimeGenerator</li>
<li>include Enumerable</li>
<li>
<li>def initialize(ubound = nil)</li>
<li>
<pre><code> @ubound = ubound
</code></pre>
</li>
<li>end</li>
<li>
<li>def upper_bound=(ubound)</li>
<li>
<pre><code> @ubound = ubound
</code></pre>
</li>
<li>end</li>
<li>def upper_bound</li>
<li>
<pre><code> @ubound
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="returns-the-next-pseudo-prime-number-and-move-the-internal"></a>
<h1 >returns the next pseudo-prime number, and move the internal<a href="#returns-the-next-pseudo-prime-number-and-move-the-internal" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="position-forward"></a>
<h1 >position forward.<a href="#position-forward" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="PseudoPrimeGeneratorsucc-raises-NotImplementedError"></a>
<h1 >+PseudoPrimeGenerator+#succ raises +NotImplementedError+.<a href="#PseudoPrimeGeneratorsucc-raises-NotImplementedError" class="wiki-anchor">¶</a></h1>
</li>
<li>def succ</li>
<li>
<pre><code> raise NotImplementedError, "need to define `succ'"
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="alias-of-succ"></a>
<h1 >alias of +succ+.<a href="#alias-of-succ" class="wiki-anchor">¶</a></h1>
</li>
<li>def next</li>
<li>
<pre><code> raise NotImplementedError, "need to define `next'"
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="Rewinds-the-internal-position-for-enumeration"></a>
<h1 >Rewinds the internal position for enumeration.<a href="#Rewinds-the-internal-position-for-enumeration" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="See-Enumeratorrewind"></a>
<h1 >See +Enumerator+#rewind.<a href="#See-Enumeratorrewind" class="wiki-anchor">¶</a></h1>
</li>
<li>def rewind</li>
<li>
<pre><code> raise NotImplementedError, "need to define `rewind'"
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="Iterates-the-given-block-for-each-prime-numbers"></a>
<h1 >Iterates the given block for each prime numbers.<a href="#Iterates-the-given-block-for-each-prime-numbers" class="wiki-anchor">¶</a></h1>
</li>
<li>def each(&block)</li>
<li>
<pre><code> return self.dup unless block
</code></pre>
</li>
<li>
<pre><code> if @ubound
</code></pre>
</li>
<li>last_value = nil</li>
<li>loop do</li>
<li>prime = succ</li>
<li>break last_value if prime > @ubound</li>
<li>last_value = block.call(prime)</li>
<li>end</li>
<li>
<pre><code> else
</code></pre>
</li>
<li>loop do</li>
<li>block.call(succ)</li>
<li>end</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="see-Enumeratorwith_index"></a>
<h1 >see +Enumerator+#with_index.<a href="#see-Enumeratorwith_index" class="wiki-anchor">¶</a></h1>
</li>
<li>alias with_index each_with_index</li>
<li>
<li>
<a name="see-Enumeratorwith_object"></a>
<h1 >see +Enumerator+#with_object.<a href="#see-Enumeratorwith_object" class="wiki-anchor">¶</a></h1>
</li>
<li>def with_object(obj)</li>
<li>
<pre><code> return enum_for(:with_object) unless block_given?
</code></pre>
</li>
<li>
<pre><code> each do |prime|
</code></pre>
</li>
<li>yield prime, obj</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>end</li>
<li>end</li>
<li>
<li>
<a name="An-implementation-of-PseudoPrimeGenerator"></a>
<h1 >An implementation of +PseudoPrimeGenerator+.<a href="#An-implementation-of-PseudoPrimeGenerator" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="Uses-EratosthenesSieve"></a>
<h1 >Uses +EratosthenesSieve+.<a href="#Uses-EratosthenesSieve" class="wiki-anchor">¶</a></h1>
</li>
<li>class EratosthenesGenerator < PseudoPrimeGenerator</li>
<li>def initialize</li>
<li>
<pre><code> @last_prime = nil
</code></pre>
</li>
<li>
<pre><code> super
</code></pre>
</li>
<li>end</li>
<li>
<li>def succ</li>
<li>
<pre><code> @last_prime = @last_prime ? EratosthenesSieve.instance.next_to(@last_prime) : 2
</code></pre>
</li>
<li>end</li>
<li>def rewind</li>
<li>
<pre><code> initialize
</code></pre>
</li>
<li>end</li>
<li>alias next succ</li>
<li>end</li>
<li>
<li>
<a name="An-implementation-of-PseudoPrimeGenerator-which-uses"></a>
<h1 >An implementation of +PseudoPrimeGenerator+ which uses<a href="#An-implementation-of-PseudoPrimeGenerator-which-uses" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="a-prime-table-generated-by-trial-division"></a>
<h1 >a prime table generated by trial division.<a href="#a-prime-table-generated-by-trial-division" class="wiki-anchor">¶</a></h1>
</li>
<li>class TrialDivisionGenerator<PseudoPrimeGenerator</li>
<li>def initialize</li>
<li>
<pre><code> @index = -1
</code></pre>
</li>
<li>
<pre><code> super
</code></pre>
</li>
<li>end</li>
<li>
<li>def succ</li>
<li>
<pre><code> TrialDivision.instance[@index += 1]
</code></pre>
</li>
<li>end</li>
<li>def rewind</li>
<li>
<pre><code> initialize
</code></pre>
</li>
<li>end</li>
<li>alias next succ</li>
<li>end</li>
<li>
<li>
<a name="Generates-all-integer-which-are-greater-than-2-and"></a>
<h1 >Generates all integer which are greater than 2 and<a href="#Generates-all-integer-which-are-greater-than-2-and" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="are-not-divided-by-2-nor-3"></a>
<h1 >are not divided by 2 nor 3.<a href="#are-not-divided-by-2-nor-3" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="This-is-a-pseudo-prime-generator-suitable-on"></a>
<h1 >This is a pseudo-prime generator, suitable on<a href="#This-is-a-pseudo-prime-generator-suitable-on" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="checking-primality-of-a-integer-by-brute-force"></a>
<h1 >checking primality of a integer by brute force<a href="#checking-primality-of-a-integer-by-brute-force" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="method"></a>
<h1 >method.<a href="#method" class="wiki-anchor">¶</a></h1>
</li>
<li>class Generator23<PseudoPrimeGenerator</li>
<li>def initialize</li>
<li>
<pre><code> @prime = 1
</code></pre>
</li>
<li>
<pre><code> @step = nil
</code></pre>
</li>
<li>
<pre><code> super
</code></pre>
</li>
<li>end</li>
<li>
<li>def succ</li>
<li>
<pre><code> loop do
</code></pre>
</li>
<li>if (@step)</li>
<li>@prime += @step</li>
<li>@step = 6 - @step</li>
<li>else</li>
<li>case @prime</li>
<li>when 1; @prime = 2</li>
<li>when 2; @prime = 3</li>
<li>when 3; @prime = 5; @step = 2</li>
<li>end</li>
<li>end</li>
<li>return @prime</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>end</li>
<li>alias next succ</li>
<li>def rewind</li>
<li>
<pre><code> initialize
</code></pre>
</li>
<li>end</li>
<li>end</li>
<li>
<li>
<li>
<li>
<li>
<a name="Internal-use-An-implementation-of-prime-table-by-trial-division-method"></a>
<h1 >Internal use. An implementation of prime table by trial division method.<a href="#Internal-use-An-implementation-of-prime-table-by-trial-division-method" class="wiki-anchor">¶</a></h1>
</li>
<li>class TrialDivision</li>
<li>include Singleton</li>
<li>
<li>def initialize # :nodoc:</li>
<li>
<pre><code> # These are included as class variables to cache them for later uses. If memory
</code></pre>
</li>
<li>
<pre><code> # usage is a problem, they can be put in Prime#initialize as instance variables.
</code></pre>
</li>
<li>
<li>
<pre><code> # There must be no primes between @primes[-1] and @next_to_check.
</code></pre>
</li>
<li>
<pre><code> @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]
</code></pre>
</li>
<li>
<pre><code> # @next_to_check % 6 must be 1.
</code></pre>
</li>
<li>
<pre><code> @next_to_check = 103 # @primes[-1] - @primes[-1] % 6 + 7
</code></pre>
</li>
<li>
<pre><code> @ulticheck_index = 3 # @primes.index(@primes.reverse.find {|n|
</code></pre>
</li>
<li>
<pre><code> # n < Math.sqrt(@@next_to_check) })
</code></pre>
</li>
<li>
<pre><code> @ulticheck_next_squared = 121 # @primes[@ulticheck_index + 1] ** 2
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="Returns-the-cached-prime-numbers"></a>
<h1 >Returns the cached prime numbers.<a href="#Returns-the-cached-prime-numbers" class="wiki-anchor">¶</a></h1>
</li>
<li>def cache</li>
<li>
<pre><code> return @primes
</code></pre>
</li>
<li>end</li>
<li>alias primes cache</li>
<li>alias primes_so_far cache</li>
<li>
<li>
<a name="Returns-the-indexth-prime-number"></a>
<h1 >Returns the +index+th prime number.<a href="#Returns-the-indexth-prime-number" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="index-is-a-0-based-index"></a>
<h1 >+index+ is a 0-based index.<a href="#index-is-a-0-based-index" class="wiki-anchor">¶</a></h1>
</li>
<li>def <a href="index"></a>
</li>
<li>
<pre><code> while index >= @primes.length
</code></pre>
</li>
<li>
<a name="Only-check-for-prime-factors-up-to-the-square-root-of-the-potential-primes"></a>
<h1 >Only check for prime factors up to the square root of the potential primes,<a href="#Only-check-for-prime-factors-up-to-the-square-root-of-the-potential-primes" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="but-without-the-performance-hit-of-an-actual-square-root-calculation"></a>
<h1 >but without the performance hit of an actual square root calculation.<a href="#but-without-the-performance-hit-of-an-actual-square-root-calculation" class="wiki-anchor">¶</a></h1>
</li>
<li>if @next_to_check + 4 > @ulticheck_next_squared</li>
<li>@ulticheck_index += 1</li>
<li>@ulticheck_next_squared = @primes.at(@ulticheck_index + 1) ** 2</li>
<li>end</li>
<li>
<a name="Only-check-numbers-congruent-to-one-and-five-modulo-six-All-others"></a>
<h1 >Only check numbers congruent to one and five, modulo six. All others<a href="#Only-check-numbers-congruent-to-one-and-five-modulo-six-All-others" class="wiki-anchor">¶</a></h1>
</li>
<li>
<li>
<a name="are-divisible-by-two-or-three-This-also-allows-us-to-skip-checking-against"></a>
<h1 >are divisible by two or three. This also allows us to skip checking against<a href="#are-divisible-by-two-or-three-This-also-allows-us-to-skip-checking-against" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="two-and-three"></a>
<h1 >two and three.<a href="#two-and-three" class="wiki-anchor">¶</a></h1>
</li>
<li>@primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?</li>
<li>@next_to_check += 4</li>
<li>@primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?</li>
<li>@next_to_check += 2</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>
<pre><code> return @primes[index]
</code></pre>
</li>
<li>end</li>
<li>end</li>
<li>
<li>
<a name="Internal-use-An-implementation-of-eratostheness-sieve"></a>
<h1 >Internal use. An implementation of eratosthenes's sieve<a href="#Internal-use-An-implementation-of-eratostheness-sieve" class="wiki-anchor">¶</a></h1>
</li>
<li>class EratosthenesSieve</li>
<li>include Singleton</li>
<li>
<li>BITS_PER_ENTRY = 16 # each entry is a set of 16-bits in a Fixnum</li>
<li>NUMS_PER_ENTRY = BITS_PER_ENTRY * 2 # twiced because even numbers are omitted</li>
<li>ENTRIES_PER_TABLE = 8</li>
<li>NUMS_PER_TABLE = NUMS_PER_ENTRY * ENTRIES_PER_TABLE</li>
<li>FILLED_ENTRY = (1 << NUMS_PER_ENTRY) - 1</li>
<li>
<li>def initialize # :nodoc:</li>
<li>
<pre><code> # bitmap for odd prime numbers less than 256.
</code></pre>
</li>
<li>
<pre><code> # For an arbitrary odd number n, @tables[i][j][k] is
</code></pre>
</li>
<li>
<pre><code> # * 1 if n is prime,
</code></pre>
</li>
<li>
<pre><code> # * 0 if n is composite,
</code></pre>
</li>
<li>
<pre><code> # where i,j,k = indices(n)
</code></pre>
</li>
<li>
<pre><code> @tables = [[0xcb6e, 0x64b4, 0x129a, 0x816d, 0x4c32, 0x864a, 0x820d, 0x2196].freeze]
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="returns-the-least-odd-prime-number-which-is-greater-than-n"></a>
<h1 >returns the least odd prime number which is greater than +n+.<a href="#returns-the-least-odd-prime-number-which-is-greater-than-n" class="wiki-anchor">¶</a></h1>
</li>
<li>def next_to(n)</li>
<li>
<pre><code> n = (n-1).div(2)*2+3 # the next odd number to given n
</code></pre>
</li>
<li>
<pre><code> table_index, integer_index, bit_index = indices(n)
</code></pre>
</li>
<li>
<pre><code> loop do
</code></pre>
</li>
<li>extend_table until @tables.length > table_index</li>
<li>for j in integer_index...ENTRIES_PER_TABLE</li>
<li>if !@tables[table_index][j].zero?</li>
<li>
<pre><code> for k in bit_index...BITS_PER_ENTRY
</code></pre>
</li>
<li>
<pre><code> return NUMS_PER_TABLE*table_index + NUMS_PER_ENTRY*j + 2*k+1 if !@tables[table_index][j][k].zero?
</code></pre>
</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>end</li>
<li>bit_index = 0</li>
<li>end</li>
<li>table_index += 1; integer_index = 0</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>end</li>
<li>
<li>private</li>
<li>
<a name="for-an-odd-number-n-returns-i-j-k-such-that-tablesijk-represents-primarity-of-the-number"></a>
<h1 >for an odd number +n+, returns (i, j, k) such that @tables[i][j][k] represents primarity of the number<a href="#for-an-odd-number-n-returns-i-j-k-such-that-tablesijk-represents-primarity-of-the-number" class="wiki-anchor">¶</a></h1>
</li>
<li>def indices(n)</li>
<li>
<pre><code> # binary digits of n: |0|1|2|3|4|5|6|7|8|9|10|11|....
</code></pre>
</li>
<li>
<pre><code> # indices: |-| k | j | i
</code></pre>
</li>
<li>
<pre><code> # because of NUMS_PER_ENTRY, NUMS_PER_TABLE
</code></pre>
</li>
<li>
<li>
<pre><code> k = (n & 0b00011111) >> 1
</code></pre>
</li>
<li>
<pre><code> j = (n & 0b11100000) >> 5
</code></pre>
</li>
<li>
<pre><code> i = n >> 8
</code></pre>
</li>
<li>
<pre><code> return i, j, k
</code></pre>
</li>
<li>end</li>
<li>
<li>def extend_table</li>
<li>
<pre><code> lbound = NUMS_PER_TABLE * @tables.length
</code></pre>
</li>
<li>
<pre><code> ubound = lbound + NUMS_PER_TABLE
</code></pre>
</li>
<li>
<pre><code> new_table = [FILLED_ENTRY] * ENTRIES_PER_TABLE # which represents primarity in lbound...ubound
</code></pre>
</li>
<li>
<pre><code> (3..Integer(Math.sqrt(ubound))).step(2) do |p|
</code></pre>
</li>
<li>i, j, k = indices(p)</li>
<li>next if @tables[i][j][k].zero?</li>
<li>
<li>start = (lbound.div(p)+1)*p # least multiple of p which is >= lbound</li>
<li>start += p if start.even?</li>
<li>(start...ubound).step(2*p) do |n|</li>
<li>_, j, k = indices(n)</li>
<li>new_table[j] &= FILLED_ENTRY^(1<<k)</li>
<li>end</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>
<pre><code> @tables << new_table.freeze
</code></pre>
</li>
<li>end</li>
<li>end</li>
<li>
<li>
<a name="Provides-a-Prime-object-with-compatibility-to-Ruby-18-when-instantiated-via-Primenew"></a>
<h1 >Provides a +Prime+ object with compatibility to Ruby 1.8 when instantiated via +Prime+.+new+.<a href="#Provides-a-Prime-object-with-compatibility-to-Ruby-18-when-instantiated-via-Primenew" class="wiki-anchor">¶</a></h1>
</li>
<li>module OldCompatibility</li>
<li>
<a name="Returns-the-next-prime-number-and-forwards-internal-pointer"></a>
<h1 >Returns the next prime number and forwards internal pointer.<a href="#Returns-the-next-prime-number-and-forwards-internal-pointer" class="wiki-anchor">¶</a></h1>
</li>
<li>def succ</li>
<li>
<pre><code> @generator.succ
</code></pre>
</li>
<li>end</li>
<li>alias next succ</li>
<li>
<li>
<a name="Overwrites-Primeeach"></a>
<h1 >Overwrites Prime#each.<a href="#Overwrites-Primeeach" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="Iterates-the-given-block-over-all-prime-numbers-Note-that-enumeration-starts-from"></a>
<h1 >Iterates the given block over all prime numbers. Note that enumeration starts from<a href="#Iterates-the-given-block-over-all-prime-numbers-Note-that-enumeration-starts-from" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="the-current-position-of-internal-pointer-not-rewound"></a>
<h1 >the current position of internal pointer, not rewound.<a href="#the-current-position-of-internal-pointer-not-rewound" class="wiki-anchor">¶</a></h1>
</li>
<li>def each(&block)</li>
<li>
<pre><code> return @generator.dup unless block_given?
</code></pre>
</li>
<li>
<pre><code> loop do
</code></pre>
</li>
<li>yield succ</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>end</li>
<li>end<br>
+end</li>
<li>
<li>
</ul>
<p>+require 'prime.so'<br>
+<br>
diff --git ext/prime/prime.c ext/prime/prime.c<br>
new file mode 100644<br>
index 0000000..384bd0c<br>
--- /dev/null<br>
+++ ext/prime/prime.c<br>
@@ -0,0 +1,97 @@<br>
+/************************************************<br>
+</p>
<ul>
<li>prime.c -</li>
<li>
<li>Copyright (C) 2010 Shota Fukumori (sora_h)</li>
<li>
</ul>
<p>+************************************************/<br>
+<br>
+#include "ruby/ruby.h"<br>
+<br>
+VALUE prime_um_value;<br>
+<br>
+static VALUE<br>
+prime_is_value_prime(int argc, VALUE *argv, VALUE self) {</p>
<ul>
<li>int step23, v, g;</li>
<li>long int i, x;</li>
<li>VALUE value, generator, t, iv;</li>
<li>
<li>rb_scan_args(argc, argv, "11", &value, &generator);</li>
<li>
<li>if(TYPE(value) == T_FLOAT)</li>
<li>return Qfalse;</li>
<li>
<li>if(!(FIXNUM_P(value) || TYPE(value) == T_BIGNUM))</li>
<li>rb_raise(rb_eTypeError, "value must be a numeric");</li>
<li>if(!prime_um_value)</li>
<li>prime_um_value = ULONG2NUM(ULONG_MAX);</li>
<li>
<li>if (!FIXNUM_P(value) && rb_funcall(value,rb_intern(">"),1,prime_um_value) == Qtrue){</li>
<li>v = 1;</li>
<li>if (rb_funcall(value,rb_intern("<"),1,INT2FIX(2)) == Qtrue)</li>
<li>
<pre><code> return Qfalse;
</code></pre>
</li>
<li>if (rb_funcall(value,rb_intern("=="),1,INT2FIX(2)) == Qtrue ||</li>
<li>
<pre><code> rb_funcall(value,rb_intern("=="),1,INT2FIX(3)) == Qtrue)
</code></pre>
</li>
<li>
<pre><code> return Qtrue;
</code></pre>
</li>
<li>} else {</li>
<li>v = 0;</li>
<li>x = NUM2LONG(value);</li>
<li>if (x < 0) x = x * -1;</li>
<li>
<li>if (x < 2) return Qfalse;</li>
<li>if (x == 2) return Qtrue;</li>
<li>if (x == 3) return Qtrue;</li>
<li>}</li>
<li>step23 = 0;</li>
<li>i = 1;</li>
<li>g = NIL_P(generator);</li>
<li>while(1) {</li>
<li>if(g) {</li>
<li>
<pre><code> if (step23 < 1) {
</code></pre>
</li>
<li>
<pre><code> switch(i) {
</code></pre>
</li>
<li>
<pre><code> case 1:
</code></pre>
</li>
<li>
<pre><code> i = 2;
</code></pre>
</li>
<li>
<pre><code> break;
</code></pre>
</li>
<li>
<pre><code> case 2:
</code></pre>
</li>
<li>
<pre><code> i = 3;
</code></pre>
</li>
<li>
<pre><code> break;
</code></pre>
</li>
<li>
<pre><code> case 3:
</code></pre>
</li>
<li>
<pre><code> i = 5;
</code></pre>
</li>
<li>
<pre><code> step23 = 2;
</code></pre>
</li>
<li>
<pre><code> break;
</code></pre>
</li>
<li>
<pre><code> }
</code></pre>
</li>
<li>
<pre><code> }else{
</code></pre>
</li>
<li>
<pre><code> i += step23;
</code></pre>
</li>
<li>
<pre><code> step23 = 6 - step23;
</code></pre>
</li>
<li>
<pre><code> }
</code></pre>
</li>
<li>}else{</li>
<li>
<pre><code> iv = rb_funcall(generator,rb_intern("succ"),0);
</code></pre>
</li>
<li>
<pre><code> if (!v) i = NUM2ULONG(iv);
</code></pre>
</li>
<li>}</li>
<li>if (v) {</li>
<li>
<pre><code> if (!g) iv = ULONG2NUM(i);
</code></pre>
</li>
<li>
<pre><code> t = rb_funcall(value,rb_intern("divmod"),1,iv);
</code></pre>
</li>
<li>
<pre><code> if (rb_funcall(rb_ary_shift(t),rb_intern("<"),1,iv) == Qtrue)
</code></pre>
</li>
<li>
<pre><code> return Qtrue;
</code></pre>
</li>
<li>
<pre><code> if (rb_funcall(rb_ary_shift(t),rb_intern("=="),1,INT2FIX(0)) == Qtrue)
</code></pre>
</li>
<li>
<pre><code> return Qfalse;
</code></pre>
</li>
<li>} else {</li>
<li>
<pre><code> if (x / i < i)
</code></pre>
</li>
<li>
<pre><code> return Qtrue;
</code></pre>
</li>
<li>
<pre><code> if (x % i == 0)
</code></pre>
</li>
<li>
<pre><code> return Qfalse;
</code></pre>
</li>
<li>}</li>
<li>}</li>
<li>/*}else{</li>
<li>i = rb</li>
<li>return Qfalse; NOTE: fix this</li>
<li>}*/<br>
+}</li>
<li>
</ul>
<p>+void<br>
+Init_prime(void) {</p>
<ul>
<li>VALUE rb_cPrime;</li>
<li>rb_cPrime = rb_define_class("Prime", rb_cObject);</li>
<li>
<li>rb_define_singleton_method(rb_cPrime, "prime?", prime_is_value_prime, -1);<br>
+}<br>
diff --git lib/prime.rb lib/prime.rb<br>
deleted file mode 100644<br>
index a40d90e..0000000<br>
--- lib/prime.rb<br>
+++ /dev/null<br>
@@ -1,495 +0,0 @@<br>
-#<br>
-# = prime.rb<br>
-#<br>
-# Prime numbers and factorization library.<br>
-#<br>
-# Copyright::<br>
-# Copyright (c) 1998-2008 Keiju ISHITSUKA(SHL Japan Inc.)<br>
-# Copyright (c) 2008 Yuki Sonoda (Yugui) <a href="mailto:yugui@yugui.jp" class="email">yugui@yugui.jp</a><br>
-#<br>
-# Documentation::<br>
-# Yuki Sonoda<br>
-#</li>
</ul>
<ul>
<li>
</ul>
<h2>-require "singleton"<br>
-require "forwardable"</h2>
<p>-class Integer</p>
<ul>
<li>
<a name="Re-composes-a-prime-factorization-and-returns-the-product-3"></a>
<h1 >Re-composes a prime factorization and returns the product.<a href="#Re-composes-a-prime-factorization-and-returns-the-product-3" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="See-Primeint_from_prime_division-for-more-details-2"></a>
<h1 >See Prime#int_from_prime_division for more details.<a href="#See-Primeint_from_prime_division-for-more-details-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def Integer.from_prime_division(pd)</li>
<li>Prime.int_from_prime_division(pd)</li>
<li>end</li>
<li>
<li>
<a name="Returns-the-factorization-of-self-2"></a>
<h1 >Returns the factorization of +self+.<a href="#Returns-the-factorization-of-self-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="See-Primeprime_division-for-more-details-2"></a>
<h1 >See Prime#prime_division for more details.<a href="#See-Primeprime_division-for-more-details-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def prime_division(generator = Prime::Generator23.new)</li>
<li>Prime.prime_division(self, generator)</li>
<li>end</li>
<li>
<li>
<a name="Returns-true-if-self-is-a-prime-number-false-for-a-composite-2"></a>
<h1 >Returns true if +self+ is a prime number, false for a composite.<a href="#Returns-true-if-self-is-a-prime-number-false-for-a-composite-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def prime?</li>
<li>Prime.prime?(self)</li>
<li>end</li>
<li>
<li>
<a name="Iterates-the-given-block-over-all-prime-numbers-3"></a>
<h1 >Iterates the given block over all prime numbers.<a href="#Iterates-the-given-block-over-all-prime-numbers-3" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="See-Primeeach-for-more-details-2"></a>
<h1 >See +Prime+#each for more details.<a href="#See-Primeeach-for-more-details-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def Integer.each_prime(ubound, &block) # :yields: prime</li>
<li>Prime.each(ubound, &block)</li>
<li>end<br>
-end</li>
<li>
</ul>
<p>-#<br>
-# The set of all prime numbers.<br>
-#<br>
-# == Example<br>
-# Prime.each(100) do |prime|<br>
-# p prime #=> 2, 3, 5, 7, 11, ...., 97<br>
-# end<br>
-#<br>
-# == Retrieving the instance<br>
-# +Prime+.new is obsolete. Now +Prime+ has the default instance and you can<br>
-# access it as +Prime+.instance.<br>
-#<br>
-# For convenience, each instance method of +Prime+.instance can be accessed<br>
-# as a class method of +Prime+.<br>
-#<br>
-# e.g.<br>
-# Prime.instance.prime?(2) #=> true<br>
-# Prime.prime?(2) #=> true<br>
-#<br>
-# == Generators<br>
-# A "generator" provides an implementation of enumerating pseudo-prime<br>
-# numbers and it remembers the position of enumeration and upper bound.<br>
-# Futhermore, it is a external iterator of prime enumeration which is<br>
-# compatible to an Enumerator.<br>
-#<br>
-# +Prime+::+PseudoPrimeGenerator+ is the base class for generators.<br>
-# There are few implementations of generator.<br>
-#<br>
-# [+Prime+::+EratosthenesGenerator+]<br>
-# Uses eratosthenes's sieve.<br>
-# [+Prime+::+TrialDivisionGenerator+]<br>
-# Uses the trial division method.<br>
-# [+Prime+::+Generator23+]<br>
-# Generates all positive integers which is not divided by 2 nor 3.<br>
-# This sequence is very bad as a pseudo-prime sequence. But this<br>
-# is faster and uses much less memory than other generators. So,<br>
-# it is suitable for factorizing an integer which is not large but<br>
-# has many prime factors. e.g. for Prime#prime? .<br>
-class Prime</p>
<ul>
<li>include Enumerable</li>
<li>@the_instance = Prime.new</li>
<li>
<li>
<a name="obsolete-Use-Primeinstance-or-class-methods-of-Prime-2"></a>
<h1 >obsolete. Use +Prime+::+instance+ or class methods of +Prime+.<a href="#obsolete-Use-Primeinstance-or-class-methods-of-Prime-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def initialize</li>
<li>@generator = EratosthenesGenerator.new</li>
<li>extend OldCompatibility</li>
<li>warn "Prime::new is obsolete. use Prime::instance or class methods of Prime."</li>
<li>end</li>
<li>
<li>class << self</li>
<li>extend Forwardable</li>
<li>include Enumerable</li>
<li>
<a name="Returns-the-default-instance-of-Prime-2"></a>
<h1 >Returns the default instance of Prime.<a href="#Returns-the-default-instance-of-Prime-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def instance; @the_instance end</li>
<li>
<li>def method_added(method) # :nodoc:</li>
<li>
<pre><code> (class<< self;self;end).def_delegator :instance, method
</code></pre>
</li>
<li>end</li>
<li>end</li>
<li>
<li>
<a name="Iterates-the-given-block-over-all-prime-numbers-4"></a>
<h1 >Iterates the given block over all prime numbers.<a href="#Iterates-the-given-block-over-all-prime-numbers-4" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Parameters-5"></a>
<h1 >== Parameters<a href="#-Parameters-5" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="ubound-3"></a>
<h1 >+ubound+::<a href="#ubound-3" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Optional-An-arbitrary-positive-number-2"></a>
<h1 >Optional. An arbitrary positive number.<a href="#Optional-An-arbitrary-positive-number-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="The-upper-bound-of-enumeration-The-method-enumerates-2"></a>
<h1 >The upper bound of enumeration. The method enumerates<a href="#The-upper-bound-of-enumeration-The-method-enumerates-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="prime-numbers-infinitely-if-ubound-is-nil-2"></a>
<h1 >prime numbers infinitely if +ubound+ is nil.<a href="#prime-numbers-infinitely-if-ubound-is-nil-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="generator-2"></a>
<h1 >+generator+::<a href="#generator-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Optional-An-implementation-of-pseudo-prime-generator-2"></a>
<h1 >Optional. An implementation of pseudo-prime generator.<a href="#Optional-An-implementation-of-pseudo-prime-generator-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Return-value-2"></a>
<h1 >== Return value<a href="#-Return-value-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="An-evaluated-value-of-the-given-block-at-the-last-time-2"></a>
<h1 >An evaluated value of the given block at the last time.<a href="#An-evaluated-value-of-the-given-block-at-the-last-time-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Or-an-enumerator-which-is-compatible-to-an-Enumerator-2"></a>
<h1 >Or an enumerator which is compatible to an +Enumerator+<a href="#Or-an-enumerator-which-is-compatible-to-an-Enumerator-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="if-no-block-given-2"></a>
<h1 >if no block given.<a href="#if-no-block-given-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Description-2"></a>
<h1 >== Description<a href="#-Description-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Calls-block-once-for-each-prime-number-passing-the-prime-as-2"></a>
<h1 >Calls +block+ once for each prime number, passing the prime as<a href="#Calls-block-once-for-each-prime-number-passing-the-prime-as-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="a-parameter-2"></a>
<h1 >a parameter.<a href="#a-parameter-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="ubound-4"></a>
<h1 >+ubound+::<a href="#ubound-4" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Upper-bound-of-prime-numbers-The-iterator-stops-after-2"></a>
<h1 >Upper bound of prime numbers. The iterator stops after<a href="#Upper-bound-of-prime-numbers-The-iterator-stops-after-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="yields-all-prime-numbers-p-lt-ubound-2"></a>
<h1 >yields all prime numbers p <= +ubound+.<a href="#yields-all-prime-numbers-p-lt-ubound-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Note-2"></a>
<h1 >== Note<a href="#-Note-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Primenew-returns-a-object-extended-by-PrimeOldCompatibility-2"></a>
<h1 >+Prime+.+new+ returns a object extended by +Prime+::+OldCompatibility+<a href="#Primenew-returns-a-object-extended-by-PrimeOldCompatibility-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="in-order-to-compatibility-to-Ruby-18-and-Primeeach-is-overwritten-2"></a>
<h1 >in order to compatibility to Ruby 1.8, and +Prime+#each is overwritten<a href="#in-order-to-compatibility-to-Ruby-18-and-Primeeach-is-overwritten-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="by-PrimeOldCompatibilityeach-2"></a>
<h1 >by +Prime+::+OldCompatibility+#+each+.<a href="#by-PrimeOldCompatibilityeach-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="Primenew-is-now-obsolete-Use-Primeinstanceeach-or-simply-2"></a>
<h1 >+Prime+.+new+ is now obsolete. Use +Prime+.+instance+.+each+ or simply<a href="#Primenew-is-now-obsolete-Use-Primeinstanceeach-or-simply-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="Primeeach-2"></a>
<h1 >+Prime+.+each+.<a href="#Primeeach-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def each(ubound = nil, generator = EratosthenesGenerator.new, &block)</li>
<li>generator.upper_bound = ubound</li>
<li>generator.each(&block)</li>
<li>end</li>
<li>
<li>
<li>
<a name="Returns-true-if-value-is-prime-false-for-a-composite-2"></a>
<h1 >Returns true if +value+ is prime, false for a composite.<a href="#Returns-true-if-value-is-prime-false-for-a-composite-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Parameters-6"></a>
<h1 >== Parameters<a href="#-Parameters-6" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="value-an-arbitrary-integer-to-be-checked-2"></a>
<h1 >+value+:: an arbitrary integer to be checked.<a href="#value-an-arbitrary-integer-to-be-checked-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="generator-optional-A-pseudo-prime-generator-2"></a>
<h1 >+generator+:: optional. A pseudo-prime generator.<a href="#generator-optional-A-pseudo-prime-generator-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def prime?(value, generator = Prime::Generator23.new)</li>
<li>value = -value if value < 0</li>
<li>return false if value < 2</li>
<li>for num in generator</li>
<li>
<pre><code> q,r = value.divmod num
</code></pre>
</li>
<li>
<pre><code> return true if q < num
</code></pre>
</li>
<li>
<pre><code> return false if r == 0
</code></pre>
</li>
<li>end</li>
<li>end</li>
<li>
<li>
<a name="Re-composes-a-prime-factorization-and-returns-the-product-4"></a>
<h1 >Re-composes a prime factorization and returns the product.<a href="#Re-composes-a-prime-factorization-and-returns-the-product-4" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Parameters-7"></a>
<h1 >== Parameters<a href="#-Parameters-7" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="pd-Array-of-pairs-of-integers-The-each-internal-2"></a>
<h1 >+pd+:: Array of pairs of integers. The each internal<a href="#pd-Array-of-pairs-of-integers-The-each-internal-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="pair-consists-of-a-prime-number-a-prime-factor--2"></a>
<h1 >pair consists of a prime number -- a prime factor --<a href="#pair-consists-of-a-prime-number-a-prime-factor--2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="and-a-natural-number-an-exponent-2"></a>
<h1 >and a natural number -- an exponent.<a href="#and-a-natural-number-an-exponent-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Example-3"></a>
<h1 >== Example<a href="#-Example-3" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="For-p_1-e_1-p_2-e_2-p_n-e_n-it-returns-2"></a>
<h1 >For [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]], it returns<a href="#For-p_1-e_1-p_2-e_2-p_n-e_n-it-returns-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="p_1e_1-p_2e_2-p_ne_n-2"></a>
<h1 >p_1<strong>e_1 * p_2</strong>e_2 * .... * p_n**e_n.<a href="#p_1e_1-p_2e_2-p_ne_n-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="Primeint_from_prime_division22-31-gt-12-2"></a>
<h1 >Prime.int_from_prime_division([[2,2], [3,1]]) #=> 12<a href="#Primeint_from_prime_division22-31-gt-12-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def int_from_prime_division(pd)</li>
<li>pd.inject(1){|value, (prime, index)|</li>
<li>
<pre><code> value *= prime**index
</code></pre>
</li>
<li>}</li>
<li>end</li>
<li>
<li>
<a name="Returns-the-factorization-of-value-2"></a>
<h1 >Returns the factorization of +value+.<a href="#Returns-the-factorization-of-value-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Parameters-8"></a>
<h1 >== Parameters<a href="#-Parameters-8" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="value-An-arbitrary-integer-2"></a>
<h1 >+value+:: An arbitrary integer.<a href="#value-An-arbitrary-integer-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="generator-Optional-A-pseudo-prime-generator-2"></a>
<h1 >+generator+:: Optional. A pseudo-prime generator.<a href="#generator-Optional-A-pseudo-prime-generator-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="generatorsucc-must-return-the-next-2"></a>
<h1 >+generator+.succ must return the next<a href="#generatorsucc-must-return-the-next-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="pseudo-prime-number-in-the-ascendent-2"></a>
<h1 >pseudo-prime number in the ascendent<a href="#pseudo-prime-number-in-the-ascendent-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="order-It-must-generate-all-prime-numbers-2"></a>
<h1 >order. It must generate all prime numbers,<a href="#order-It-must-generate-all-prime-numbers-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="but-may-generate-non-prime-numbers-2"></a>
<h1 >but may generate non prime numbers.<a href="#but-may-generate-non-prime-numbers-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Exceptions-2"></a>
<h1 >=== Exceptions<a href="#-Exceptions-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="ZeroDivisionError-when-value-is-zero-2"></a>
<h1 >+ZeroDivisionError+:: when +value+ is zero.<a href="#ZeroDivisionError-when-value-is-zero-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="-Example-4"></a>
<h1 >== Example<a href="#-Example-4" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="For-an-arbitrary-integer-2"></a>
<h1 >For an arbitrary integer<a href="#For-an-arbitrary-integer-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="n-p_1e_1-p_2e_2-p_ne_n-2"></a>
<h1 >n = p_1<strong>e_1 * p_2</strong>e_2 * .... * p_n**e_n,<a href="#n-p_1e_1-p_2e_2-p_ne_n-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="prime_divisionn-returns-2"></a>
<h1 >prime_division(n) returns<a href="#prime_divisionn-returns-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="p_1-e_1-p_2-e_2-p_n-e_n-2"></a>
<h1 >[[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]].<a href="#p_1-e_1-p_2-e_2-p_n-e_n-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="Primeprime_division12-gt-22-31-2"></a>
<h1 >Prime.prime_division(12) #=> [[2,2], [3,1]]<a href="#Primeprime_division12-gt-22-31-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>def prime_division(value, generator= Prime::Generator23.new)</li>
<li>raise ZeroDivisionError if value == 0</li>
<li>if value < 0</li>
<li>
<pre><code> value = -value
</code></pre>
</li>
<li>
<pre><code> pv = [[-1, 1]]
</code></pre>
</li>
<li>else</li>
<li>
<pre><code> pv = []
</code></pre>
</li>
<li>end</li>
<li>for prime in generator</li>
<li>
<pre><code> count = 0
</code></pre>
</li>
<li>
<pre><code> while (value1, mod = value.divmod(prime)
</code></pre>
</li>
<li>
<pre><code> mod) == 0
</code></pre>
</li>
<li>value = value1</li>
<li>count += 1</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>
<pre><code> if count != 0
</code></pre>
</li>
<li>pv.push [prime, count]</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>
<pre><code> break if value1 <= prime
</code></pre>
</li>
<li>end</li>
<li>if value > 1</li>
<li>
<pre><code> pv.push [value, 1]
</code></pre>
</li>
<li>end</li>
<li>return pv</li>
<li>end</li>
<li>
<li>
<a name="An-abstract-class-for-enumerating-pseudo-prime-numbers-2"></a>
<h1 >An abstract class for enumerating pseudo-prime numbers.<a href="#An-abstract-class-for-enumerating-pseudo-prime-numbers-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="Concrete-subclasses-should-override-succ-next-rewind-2"></a>
<h1 >Concrete subclasses should override succ, next, rewind.<a href="#Concrete-subclasses-should-override-succ-next-rewind-2" class="wiki-anchor">¶</a></h1>
</li>
<li>class PseudoPrimeGenerator</li>
<li>include Enumerable</li>
<li>
<li>def initialize(ubound = nil)</li>
<li>
<pre><code> @ubound = ubound
</code></pre>
</li>
<li>end</li>
<li>
<li>def upper_bound=(ubound)</li>
<li>
<pre><code> @ubound = ubound
</code></pre>
</li>
<li>end</li>
<li>def upper_bound</li>
<li>
<pre><code> @ubound
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="returns-the-next-pseudo-prime-number-and-move-the-internal-2"></a>
<h1 >returns the next pseudo-prime number, and move the internal<a href="#returns-the-next-pseudo-prime-number-and-move-the-internal-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="position-forward-2"></a>
<h1 >position forward.<a href="#position-forward-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="PseudoPrimeGeneratorsucc-raises-NotImplementedError-2"></a>
<h1 >+PseudoPrimeGenerator+#succ raises +NotImplementedError+.<a href="#PseudoPrimeGeneratorsucc-raises-NotImplementedError-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def succ</li>
<li>
<pre><code> raise NotImplementedError, "need to define `succ'"
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="alias-of-succ-2"></a>
<h1 >alias of +succ+.<a href="#alias-of-succ-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def next</li>
<li>
<pre><code> raise NotImplementedError, "need to define `next'"
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="Rewinds-the-internal-position-for-enumeration-2"></a>
<h1 >Rewinds the internal position for enumeration.<a href="#Rewinds-the-internal-position-for-enumeration-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="See-Enumeratorrewind-2"></a>
<h1 >See +Enumerator+#rewind.<a href="#See-Enumeratorrewind-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def rewind</li>
<li>
<pre><code> raise NotImplementedError, "need to define `rewind'"
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="Iterates-the-given-block-for-each-prime-numbers-2"></a>
<h1 >Iterates the given block for each prime numbers.<a href="#Iterates-the-given-block-for-each-prime-numbers-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def each(&block)</li>
<li>
<pre><code> return self.dup unless block
</code></pre>
</li>
<li>
<pre><code> if @ubound
</code></pre>
</li>
<li>last_value = nil</li>
<li>loop do</li>
<li>prime = succ</li>
<li>break last_value if prime > @ubound</li>
<li>last_value = block.call(prime)</li>
<li>end</li>
<li>
<pre><code> else
</code></pre>
</li>
<li>loop do</li>
<li>block.call(succ)</li>
<li>end</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="see-Enumeratorwith_index-2"></a>
<h1 >see +Enumerator+#with_index.<a href="#see-Enumeratorwith_index-2" class="wiki-anchor">¶</a></h1>
</li>
<li>alias with_index each_with_index</li>
<li>
<li>
<a name="see-Enumeratorwith_object-2"></a>
<h1 >see +Enumerator+#with_object.<a href="#see-Enumeratorwith_object-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def with_object(obj)</li>
<li>
<pre><code> return enum_for(:with_object) unless block_given?
</code></pre>
</li>
<li>
<pre><code> each do |prime|
</code></pre>
</li>
<li>yield prime, obj</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>end</li>
<li>end</li>
<li>
<li>
<a name="An-implementation-of-PseudoPrimeGenerator-2"></a>
<h1 >An implementation of +PseudoPrimeGenerator+.<a href="#An-implementation-of-PseudoPrimeGenerator-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="Uses-EratosthenesSieve-2"></a>
<h1 >Uses +EratosthenesSieve+.<a href="#Uses-EratosthenesSieve-2" class="wiki-anchor">¶</a></h1>
</li>
<li>class EratosthenesGenerator < PseudoPrimeGenerator</li>
<li>def initialize</li>
<li>
<pre><code> @last_prime = nil
</code></pre>
</li>
<li>
<pre><code> super
</code></pre>
</li>
<li>end</li>
<li>
<li>def succ</li>
<li>
<pre><code> @last_prime = @last_prime ? EratosthenesSieve.instance.next_to(@last_prime) : 2
</code></pre>
</li>
<li>end</li>
<li>def rewind</li>
<li>
<pre><code> initialize
</code></pre>
</li>
<li>end</li>
<li>alias next succ</li>
<li>end</li>
<li>
<li>
<a name="An-implementation-of-PseudoPrimeGenerator-which-uses-2"></a>
<h1 >An implementation of +PseudoPrimeGenerator+ which uses<a href="#An-implementation-of-PseudoPrimeGenerator-which-uses-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="a-prime-table-generated-by-trial-division-2"></a>
<h1 >a prime table generated by trial division.<a href="#a-prime-table-generated-by-trial-division-2" class="wiki-anchor">¶</a></h1>
</li>
<li>class TrialDivisionGenerator<PseudoPrimeGenerator</li>
<li>def initialize</li>
<li>
<pre><code> @index = -1
</code></pre>
</li>
<li>
<pre><code> super
</code></pre>
</li>
<li>end</li>
<li>
<li>def succ</li>
<li>
<pre><code> TrialDivision.instance[@index += 1]
</code></pre>
</li>
<li>end</li>
<li>def rewind</li>
<li>
<pre><code> initialize
</code></pre>
</li>
<li>end</li>
<li>alias next succ</li>
<li>end</li>
<li>
<li>
<a name="Generates-all-integer-which-are-greater-than-2-and-2"></a>
<h1 >Generates all integer which are greater than 2 and<a href="#Generates-all-integer-which-are-greater-than-2-and-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="are-not-divided-by-2-nor-3-2"></a>
<h1 >are not divided by 2 nor 3.<a href="#are-not-divided-by-2-nor-3-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="This-is-a-pseudo-prime-generator-suitable-on-2"></a>
<h1 >This is a pseudo-prime generator, suitable on<a href="#This-is-a-pseudo-prime-generator-suitable-on-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="checking-primality-of-a-integer-by-brute-force-2"></a>
<h1 >checking primality of a integer by brute force<a href="#checking-primality-of-a-integer-by-brute-force-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="method-2"></a>
<h1 >method.<a href="#method-2" class="wiki-anchor">¶</a></h1>
</li>
<li>class Generator23<PseudoPrimeGenerator</li>
<li>def initialize</li>
<li>
<pre><code> @prime = 1
</code></pre>
</li>
<li>
<pre><code> @step = nil
</code></pre>
</li>
<li>
<pre><code> super
</code></pre>
</li>
<li>end</li>
<li>
<li>def succ</li>
<li>
<pre><code> loop do
</code></pre>
</li>
<li>if (@step)</li>
<li>@prime += @step</li>
<li>@step = 6 - @step</li>
<li>else</li>
<li>case @prime</li>
<li>when 1; @prime = 2</li>
<li>when 2; @prime = 3</li>
<li>when 3; @prime = 5; @step = 2</li>
<li>end</li>
<li>end</li>
<li>return @prime</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>end</li>
<li>alias next succ</li>
<li>def rewind</li>
<li>
<pre><code> initialize
</code></pre>
</li>
<li>end</li>
<li>end</li>
<li>
<li>
<li>
<li>
<li>
<a name="Internal-use-An-implementation-of-prime-table-by-trial-division-method-2"></a>
<h1 >Internal use. An implementation of prime table by trial division method.<a href="#Internal-use-An-implementation-of-prime-table-by-trial-division-method-2" class="wiki-anchor">¶</a></h1>
</li>
<li>class TrialDivision</li>
<li>include Singleton</li>
<li>
<li>def initialize # :nodoc:</li>
<li>
<pre><code> # These are included as class variables to cache them for later uses. If memory
</code></pre>
</li>
<li>
<pre><code> # usage is a problem, they can be put in Prime#initialize as instance variables.
</code></pre>
</li>
<li>
<li>
<pre><code> # There must be no primes between @primes[-1] and @next_to_check.
</code></pre>
</li>
<li>
<pre><code> @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]
</code></pre>
</li>
<li>
<pre><code> # @next_to_check % 6 must be 1.
</code></pre>
</li>
<li>
<pre><code> @next_to_check = 103 # @primes[-1] - @primes[-1] % 6 + 7
</code></pre>
</li>
<li>
<pre><code> @ulticheck_index = 3 # @primes.index(@primes.reverse.find {|n|
</code></pre>
</li>
<li>
<pre><code> # n < Math.sqrt(@@next_to_check) })
</code></pre>
</li>
<li>
<pre><code> @ulticheck_next_squared = 121 # @primes[@ulticheck_index + 1] ** 2
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="Returns-the-cached-prime-numbers-2"></a>
<h1 >Returns the cached prime numbers.<a href="#Returns-the-cached-prime-numbers-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def cache</li>
<li>
<pre><code> return @primes
</code></pre>
</li>
<li>end</li>
<li>alias primes cache</li>
<li>alias primes_so_far cache</li>
<li>
<li>
<a name="Returns-the-indexth-prime-number-2"></a>
<h1 >Returns the +index+th prime number.<a href="#Returns-the-indexth-prime-number-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="index-is-a-0-based-index-2"></a>
<h1 >+index+ is a 0-based index.<a href="#index-is-a-0-based-index-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def <a href="index"></a>
</li>
<li>
<pre><code> while index >= @primes.length
</code></pre>
</li>
<li>
<a name="Only-check-for-prime-factors-up-to-the-square-root-of-the-potential-primes-2"></a>
<h1 >Only check for prime factors up to the square root of the potential primes,<a href="#Only-check-for-prime-factors-up-to-the-square-root-of-the-potential-primes-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="but-without-the-performance-hit-of-an-actual-square-root-calculation-2"></a>
<h1 >but without the performance hit of an actual square root calculation.<a href="#but-without-the-performance-hit-of-an-actual-square-root-calculation-2" class="wiki-anchor">¶</a></h1>
</li>
<li>if @next_to_check + 4 > @ulticheck_next_squared</li>
<li>@ulticheck_index += 1</li>
<li>@ulticheck_next_squared = @primes.at(@ulticheck_index + 1) ** 2</li>
<li>end</li>
<li>
<a name="Only-check-numbers-congruent-to-one-and-five-modulo-six-All-others-2"></a>
<h1 >Only check numbers congruent to one and five, modulo six. All others<a href="#Only-check-numbers-congruent-to-one-and-five-modulo-six-All-others-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<li>
<a name="are-divisible-by-two-or-three-This-also-allows-us-to-skip-checking-against-2"></a>
<h1 >are divisible by two or three. This also allows us to skip checking against<a href="#are-divisible-by-two-or-three-This-also-allows-us-to-skip-checking-against-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="two-and-three-2"></a>
<h1 >two and three.<a href="#two-and-three-2" class="wiki-anchor">¶</a></h1>
</li>
<li>@primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?</li>
<li>@next_to_check += 4</li>
<li>@primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?</li>
<li>@next_to_check += 2</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>
<pre><code> return @primes[index]
</code></pre>
</li>
<li>end</li>
<li>end</li>
<li>
<li>
<a name="Internal-use-An-implementation-of-eratostheness-sieve-2"></a>
<h1 >Internal use. An implementation of eratosthenes's sieve<a href="#Internal-use-An-implementation-of-eratostheness-sieve-2" class="wiki-anchor">¶</a></h1>
</li>
<li>class EratosthenesSieve</li>
<li>include Singleton</li>
<li>
<li>BITS_PER_ENTRY = 16 # each entry is a set of 16-bits in a Fixnum</li>
<li>NUMS_PER_ENTRY = BITS_PER_ENTRY * 2 # twiced because even numbers are omitted</li>
<li>ENTRIES_PER_TABLE = 8</li>
<li>NUMS_PER_TABLE = NUMS_PER_ENTRY * ENTRIES_PER_TABLE</li>
<li>FILLED_ENTRY = (1 << NUMS_PER_ENTRY) - 1</li>
<li>
<li>def initialize # :nodoc:</li>
<li>
<pre><code> # bitmap for odd prime numbers less than 256.
</code></pre>
</li>
<li>
<pre><code> # For an arbitrary odd number n, @tables[i][j][k] is
</code></pre>
</li>
<li>
<pre><code> # * 1 if n is prime,
</code></pre>
</li>
<li>
<pre><code> # * 0 if n is composite,
</code></pre>
</li>
<li>
<pre><code> # where i,j,k = indices(n)
</code></pre>
</li>
<li>
<pre><code> @tables = [[0xcb6e, 0x64b4, 0x129a, 0x816d, 0x4c32, 0x864a, 0x820d, 0x2196].freeze]
</code></pre>
</li>
<li>end</li>
<li>
<li>
<a name="returns-the-least-odd-prime-number-which-is-greater-than-n-2"></a>
<h1 >returns the least odd prime number which is greater than +n+.<a href="#returns-the-least-odd-prime-number-which-is-greater-than-n-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def next_to(n)</li>
<li>
<pre><code> n = (n-1).div(2)*2+3 # the next odd number to given n
</code></pre>
</li>
<li>
<pre><code> table_index, integer_index, bit_index = indices(n)
</code></pre>
</li>
<li>
<pre><code> loop do
</code></pre>
</li>
<li>extend_table until @tables.length > table_index</li>
<li>for j in integer_index...ENTRIES_PER_TABLE</li>
<li>if !@tables[table_index][j].zero?</li>
<li>
<pre><code> for k in bit_index...BITS_PER_ENTRY
</code></pre>
</li>
<li>
<pre><code> return NUMS_PER_TABLE*table_index + NUMS_PER_ENTRY*j + 2*k+1 if !@tables[table_index][j][k].zero?
</code></pre>
</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>end</li>
<li>bit_index = 0</li>
<li>end</li>
<li>table_index += 1; integer_index = 0</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>end</li>
<li>
<li>private</li>
<li>
<a name="for-an-odd-number-n-returns-i-j-k-such-that-tablesijk-represents-primarity-of-the-number-2"></a>
<h1 >for an odd number +n+, returns (i, j, k) such that @tables[i][j][k] represents primarity of the number<a href="#for-an-odd-number-n-returns-i-j-k-such-that-tablesijk-represents-primarity-of-the-number-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def indices(n)</li>
<li>
<pre><code> # binary digits of n: |0|1|2|3|4|5|6|7|8|9|10|11|....
</code></pre>
</li>
<li>
<pre><code> # indices: |-| k | j | i
</code></pre>
</li>
<li>
<pre><code> # because of NUMS_PER_ENTRY, NUMS_PER_TABLE
</code></pre>
</li>
<li>
<li>
<pre><code> k = (n & 0b00011111) >> 1
</code></pre>
</li>
<li>
<pre><code> j = (n & 0b11100000) >> 5
</code></pre>
</li>
<li>
<pre><code> i = n >> 8
</code></pre>
</li>
<li>
<pre><code> return i, j, k
</code></pre>
</li>
<li>end</li>
<li>
<li>def extend_table</li>
<li>
<pre><code> lbound = NUMS_PER_TABLE * @tables.length
</code></pre>
</li>
<li>
<pre><code> ubound = lbound + NUMS_PER_TABLE
</code></pre>
</li>
<li>
<pre><code> new_table = [FILLED_ENTRY] * ENTRIES_PER_TABLE # which represents primarity in lbound...ubound
</code></pre>
</li>
<li>
<pre><code> (3..Integer(Math.sqrt(ubound))).step(2) do |p|
</code></pre>
</li>
<li>i, j, k = indices(p)</li>
<li>next if @tables[i][j][k].zero?</li>
<li>
<li>start = (lbound.div(p)+1)*p # least multiple of p which is >= lbound</li>
<li>start += p if start.even?</li>
<li>(start...ubound).step(2*p) do |n|</li>
<li>_, j, k = indices(n)</li>
<li>new_table[j] &= FILLED_ENTRY^(1<<k)</li>
<li>end</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>
<pre><code> @tables << new_table.freeze
</code></pre>
</li>
<li>end</li>
<li>end</li>
<li>
<li>
<a name="Provides-a-Prime-object-with-compatibility-to-Ruby-18-when-instantiated-via-Primenew-2"></a>
<h1 >Provides a +Prime+ object with compatibility to Ruby 1.8 when instantiated via +Prime+.+new+.<a href="#Provides-a-Prime-object-with-compatibility-to-Ruby-18-when-instantiated-via-Primenew-2" class="wiki-anchor">¶</a></h1>
</li>
<li>module OldCompatibility</li>
<li>
<a name="Returns-the-next-prime-number-and-forwards-internal-pointer-2"></a>
<h1 >Returns the next prime number and forwards internal pointer.<a href="#Returns-the-next-prime-number-and-forwards-internal-pointer-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def succ</li>
<li>
<pre><code> @generator.succ
</code></pre>
</li>
<li>end</li>
<li>alias next succ</li>
<li>
<li>
<a name="Overwrites-Primeeach-2"></a>
<h1 >Overwrites Prime#each.<a href="#Overwrites-Primeeach-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<h1></h1>
</li>
<li>
<a name="Iterates-the-given-block-over-all-prime-numbers-Note-that-enumeration-starts-from-2"></a>
<h1 >Iterates the given block over all prime numbers. Note that enumeration starts from<a href="#Iterates-the-given-block-over-all-prime-numbers-Note-that-enumeration-starts-from-2" class="wiki-anchor">¶</a></h1>
</li>
<li>
<a name="the-current-position-of-internal-pointer-not-rewound-2"></a>
<h1 >the current position of internal pointer, not rewound.<a href="#the-current-position-of-internal-pointer-not-rewound-2" class="wiki-anchor">¶</a></h1>
</li>
<li>def each(&block)</li>
<li>
<pre><code> return @generator.dup unless block_given?
</code></pre>
</li>
<li>
<pre><code> loop do
</code></pre>
</li>
<li>yield succ</li>
<li>
<pre><code> end
</code></pre>
</li>
<li>end</li>
<li>end<br>
-end<br>
diff --git test/test_prime.rb test/test_prime.rb<br>
index e095a29..eba8bcd 100644<br>
--- test/test_prime.rb<br>
+++ test/test_prime.rb<br>
@@ -95,6 +95,13 @@ class TestPrime < Test::Unit::TestCase<br>
assert !Prime.instance.respond_to?(:next)<br>
end</li>
</ul>
<ul>
<li>def test_prime?</li>
<li>
<a name="force-use-PrimeGenerator23-for-generator"></a>
<h1 >force use Prime::Generator23 for generator<a href="#force-use-PrimeGenerator23-for-generator" class="wiki-anchor">¶</a></h1>
</li>
<li>assert !Prime.prime?(0,Prime::Generator23.new)</li>
<li>assert !Prime.prime?(1,Prime::Generator23.new)</li>
<li>assert Prime.prime?(7,Prime::Generator23.new)</li>
<li>end</li>
<li>class TestInteger < Test::Unit::TestCase<br>
def test_prime_division<br>
pd = PRIMES.inject(&:*).prime_division</li>
</ul>
<p>=end</p>
Ruby master - Feature #3814: Prime.prime?をC拡張にする
https://bugs.ruby-lang.org/issues/3814?journal_id=13254
2010-09-10T17:43:01Z
naruse (Yui NARUSE)
naruse@airemix.jp
<ul></ul><p>=begin<br>
このアルゴリズムでも4秒になるんですねぇ。</p>
<p>で、prime.rb はかなり素朴なアルゴリズムなので、これに速度を求めるのがそもそもの間違いな気がします。<br>
Pure Rubyでもまだまだ頑張る余地があるわけで、まずよりよいアルゴリズムを用いるべきでしょう。</p>
<p>また、どうせほとんどは false なのですから、まずいくつか確率的に判定してみるのもいいんじゃないですかね。<br>
<a href="http://deztec.jp/x/05/faireal/faireal-7-03-index.html" class="external">http://deztec.jp/x/05/faireal/faireal-7-03-index.html</a></p>
<p>なお、ほんとうに早いのが欲しいのでしたら、OpenSSL::BNを使うのが正解だと思うので、<br>
これを使うようにしたほうがいいでしょう。<br>
=end</p>
Ruby master - Feature #3814: Prime.prime?をC拡張にする
https://bugs.ruby-lang.org/issues/3814?journal_id=13426
2010-09-14T17:20:29Z
shyouhei (Shyouhei Urabe)
shyouhei@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>=begin<br>
いったんrejectします。Pure ruby版はまだ改良できます。まずはそちらの方向で頑張ってもらったほうが他の実装にも良いはず。<br>
=end</p>