Граф коммитов

47 Коммитов

Автор SHA1 Сообщение Дата
Marc-Andre Lafortune b603085d2f [ruby/prime] v0.1.2 2020-12-09 00:45:56 -05:00
Marc-Andre Lafortune 1866d483dc [ruby/prime] Optimize `Integer#prime?`
Miller Rabin algorithm can be used to test primality for integers smaller than a max value "MaxMR" (~3e24)

It can be much faster than previous implementation: ~100x faster for numbers with 13 digits, at least 5 orders of magnitude for even larger numbers (previous implementation is so slow that it requires more patience than I have for more precise estimate).

Miller Rabin test becomes faster than previous implementation at somewhere in the range 1e5-1e6. It seems that the range 62000..66000 is where Miller Rabin starts being always faster, so I picked 0xffff arbitrarily; before that, or above "MaxMR", the previous implementation remains.

I compared the `faster_prime` gem too. It is slower than previous implementation up to ~1e4. After that it becomes faster and faster compared to previous implementation, but is still slower than Miller Rabin starting at ~1e5 and up to MaxMR. Thus, after this commit, builtin `Integer#prime?` will be similar or faster than `faster_prime` up to "MaxMR".

Adapted from patch of Stephen Blackstone [Feature #16468]

Benchmark results and code: https://gist.github.com/marcandre/b263bdae488e76dabdda84daf73733b9

Co-authored-by: Stephen Blackstone <sblackstone@gmail.com>
2020-12-09 00:40:09 -05:00
Marcus Stollsteimer baaf681570
Improve docs for Prime.{prime_division,int_from_prime_division} (#8)
Move explanation for the decomposition array from the Example section
to the method description. Mention the term "multiplicity".

Use examples that also demonstrate factors with multiplicity
other than 1, and avoid factors/multiplicities with the same value.
Also add the decomposition written as simple mathematical expression.

This also fixes missing syntax highlighting for the code examples
due to verbatim blocks that did not only include Ruby code.
2020-03-06 20:55:22 +09:00
Marcus Stollsteimer e92fbaf609 [ruby/prime] Fix typo
https://github.com/ruby/prime/commit/549c1b86f1
2020-03-06 20:54:45 +09:00
Marcus Stollsteimer f236f6082c [ruby/prime] Improve docs for Prime.include? (#7)
https://github.com/ruby/prime/commit/230a5af325
2020-03-06 20:54:38 +09:00
Jeremy Evans e79fc05a4c [ruby/prime] Fix Prime.include?
Previously, it would be an infinite loop if passed a non-prime
integer.

Also, Prime.include? should also provide similar results to
Module#include? if passed a Module, so handle that.

For consistency with Enumerable#include?, return false if passed
other object types.

Fixes Ruby Bug 10167.

https://github.com/ruby/prime/commit/55dda6aa7f
2020-03-06 20:54:33 +09:00
Marc-Andre Lafortune 048f797bf0 [ruby/prime] Bump version 2019-12-22 21:48:54 -05:00
nobu 9f1fb0a17f proc.c: proc without block
* proc.c (proc_new): promoted lambda/proc/Proc.new with no block
  in a method called with a block to a warning/error.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66772 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-10 08:19:14 +00:00
mame 103b5063f2 lib/prime.rb: remove unused methods
This change removes TrialDivision#cache, TrialDivision#primes, and
TrialDivision#primes_so_far.

TrialDivision class is undocumented officially, so this class is used
only internally.  Yugui san moved prime library from mathn to prime in
2008, and then she might forget to delete these methods.

A patch from @shio-phys.  https://github.com/ruby/prime/pull/4

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63737 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-24 09:31:07 +00:00
hsbt 8ce71b6160 Promote Prime library to default gems.
* Its upstream is https://github.com/ruby/prime.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63560 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-02 23:05:45 +00:00
stomar 82092f4bb7 prime.rb: remove alias after timeout test
* test/test_prime.rb: remove alias after timeout test.

* lib/prime.rb: fix typo.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58813 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-20 10:21:44 +00:00
marcandre 68354c350a lib/prime: Fix primality of some large integers [#13492].
* lib/prime.rb: Use accurate sqrt to insure all factors are tested.
  Patch by Marcus Stollsteimer.

* test/test_prime.rb: Adapt test for timeout

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58809 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-20 00:36:55 +00:00
hsbt 6b35c34c68 * basictest/test.rb: Adjust spaces in class declarations
with inheritance. [fix GH-1227] Patch by @adrfer
* lib/irb/*: ditto.
* lib/prime.rb: ditto.
* lib/shell/builtin-command.rb: ditto.
* object.c: ditto.
* sample/*.rb: ditto.
* test/-ext-/method/test_arity.rb: ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56371 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-10-07 05:18:57 +00:00
marcandre 2401c512ef * lib/prime.rb: Optimize prime?
Adapted from patch by Jabari Zakiya [#12665]

* test/test_prime.rb: Improve test

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55856 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-08-10 18:17:41 +00:00
naruse 3e92b635fb Add frozen_string_literal: false for all files
When you change this to true, you may need to add more tests.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53141 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-16 05:07:31 +00:00
marcandre 487736646a * lib/prime.rb: Add basic argument checking to Prime.prime?
[Bug #11606]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52201 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-10-20 03:19:44 +00:00
marcandre 8744d5164d * lib/prime.rb: Optimize Integer#prime?
Patch by Nick Slocum [Bug #10354]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52200 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-10-20 03:18:14 +00:00
marcandre b28f3e5c4f * lib/prime.rb: Return sized enumerators.
Patch by Kenichi Kamiya [GH-931]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50857 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-12 15:45:59 +00:00
marcandre 5b78e48c31 * lib/prime.rb: Fix with_object with no block given
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50856 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-12 15:45:44 +00:00
marcandre dd8f914d3b * lib/prime.rb: Have with_index accept an offset parameter.
Based on patch by T Yamada. [#11007]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50854 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-12 15:45:22 +00:00
marcandre 7ea2bb3235 * lib/prime.rb: Simplify and optimize EratosthenesSieve
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50803 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-09 03:32:45 +00:00
marcandre 9d3c64a40a * lib/prime.rb: Simplify and optimize EratosthenesSieve
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50802 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-09 03:32:11 +00:00
marcandre 3ed18fa817 * lib/matrix.rb: Simplify and optimize EratosthenesSieve
based on patch by Ajay Kumar. [Fixes GH-921]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50800 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-09 02:45:31 +00:00
marcandre 2c686723fe * lib/matrix.rb: Styling
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50605 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-05-22 13:37:02 +00:00
marcandre d2487ed475 * lib/prime.rb: Remove obsolete Prime.new
patch by Ajay Kumar. [Fixes GH-891]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50604 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-05-22 13:36:39 +00:00
marcandre 0eebb8f1c0 * lib/prime.rb: Remove useless loop and block capture.
See [#10354]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48767 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-12-10 20:38:13 +00:00
hsbt 448c87008e * lib/cgi/core.rb: remove unused variables.
* lib/erb.rb: ditto.
* lib/mkmf.rb: ditto.
* lib/net/http/response.rb: ditto.
* lib/optparse/version.rb: ditto.
* lib/prime.rb: ditto.
* lib/racc/parser.rb: ditto.
* lib/rexml/document.rb: ditto.
* lib/rexml/dtd/dtd.rb: ditto.
* lib/rexml/element.rb: ditto.
* lib/rexml/functions.rb: ditto.
* lib/rexml/parsers/xpathparser.rb: ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46973 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-07-27 11:04:28 +00:00
nobu 598a95b3eb prime.rb: fix a grammo
* lib/prime.rb (prime?): [DOC] fix a grammo, a missing article.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46076 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-05-24 08:14:20 +00:00
ayumin 212ea12e3a add information of incompatibility about Prime.prime? [Bug #7395]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46061 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-05-23 15:32:19 +00:00
ayumin bef8e4557c * lib/prime.rb (Prime#prime?): negative numbers can't be primes
by definition. reported by Ivan Kataitsev. [Bug #7395]
* test/test_prime.rb: add test.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45878 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-05-08 18:22:29 +00:00
yugui def83bff91 * lib/prime.rb (Prime::EratosthenesGenerator,
Prime::EratosthenesSieve): New implementation by
  robertjlooby <robertjlooby AT gmail.com>.

* test/test_prime.rb: updated with new method name

commit 4b6090ea852d63b26e02796c69b41caa0fa95077
Merge: ceda881 c8f7809
Author: Yuki Sonoda (Yugui) <yugui@yugui.jp>
Date:   Mon Jul 15 12:50:04 2013 +0900

    Merge commit 'c8f780987fbdfbae428977487e1cf793c4c36d3f'

    Conflicts:
    	lib/prime.rb

commit c8f780987fbdfbae428977487e1cf793c4c36d3f
Author: robertjlooby <robertjlooby@gmail.com>
Date:   Thu Jun 27 23:04:45 2013 -0500

    updated test/test_prime.rb with new method name

commit 996517bdbb3108cd1687d99613b69e539eb1567b
Author: robertjlooby <robertjlooby@gmail.com>
Date:   Thu Jun 27 22:59:39 2013 -0500

    new implementation of Prime::EratosthenesGenerator and Prime::EratosthenesSieve

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41982 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-07-15 04:21:34 +00:00
charliesome 3628ab8092 * lib/prime.rb: Corrected a few comments. Patch by @Nullset14.
Fixes GH-346.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41714 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-06-30 13:40:10 +00:00
zzak 790e7f797b * lib/irb.rb, lib/prime.rb: Typos in overview
Patch by Ershad K [Github Fixes #234]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38798 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-01-13 05:07:08 +00:00
drbrain b6dd727b86 * 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
2011-06-01 00:21:46 +00:00
nobu d371e3583e * lib: revert r31635-r31638 and untabify with expand(1).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31641 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-19 00:07:25 +00:00
drbrain 7bbf2f3085 * lib: Convert tabs to spaces for ruby files per
http://redmine.ruby-lang.org/projects/ruby/wiki/DeveloperHowto#coding-style
	  Patch by Steve Klabnik [Ruby 1.9 - Bug #4730]
	  Patch by Jason Dew [Ruby 1.9 - Feature #4718]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31635 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-18 21:19:18 +00:00
nobu c9dd4823d9 * lib: fixed typo. a patch by Sho Hashimoto in [ruby-dev:40716].
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26986 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-03-20 03:30:59 +00:00
yugui c0b42eedea * test/test_prime.rb
(TestPrime#test_eratosthenes_works_fine_after_timeout):
  test for [ruby-dev:39465].

* lib/prime.rb (Prime::EratosthenesSieve):
  fixed [ruby-dev:39465].
  suppressed memory reallocation.
  constantified some magic numbers.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25388 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-18 00:55:34 +00:00
nobu a8998030fb * lib/prime.rb (EratosthenesGenerator#initialize): call super.
(TrialDivisionGenerator, Generator23): ditto.  [ruby-core:25539]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24884 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-09-13 00:38:07 +00:00
yugui 7287b852da * lib/prime.rb (Prime#prime_division): now decomposes
negative integer into a decomposition with element [-1, 1].

* test/test_prime.rb: test for it.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24091 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-07-13 13:51:49 +00:00
matz cf8c9c3dd1 * lib/prime.rb: documentation typo fixed. a patch from okkez.
[ruby-dev:38586]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23665 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-06-11 06:29:06 +00:00
nobu 287a34ae0d * {ext,lib,test}/**/*.rb: removed trailing spaces.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22784 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-06 03:56:38 +00:00
yugui 294f45b0fc * lib/prime.rb (Prime::prime?): used to return a wrong answer.
[ruby-core:22646].

* test/test_prime.rb (test_prime?): test case for [ruby-core:22646].

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22741 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-03 15:57:15 +00:00
matz 85bae86cb6 * lib/optparse/version.rb: remove variable shadowing to stop
warning.   [ruby-core:20612]

* lib/irb/completion.rb, lib/net/imap.rb, lib/prime.rb,
  lib/rinda/ring.rb, lib/racc/parser.rb,
  lib/shell/command-processor.rb, lib/yaml/yamlnode.rb: ditto.

* lib/racc/parser.rb: remove space before parentheses.

* lib/shell/command-processor.rb, lib/shell/process-controller.rb:
  use parentheses around arguments.

* lib/irb/ext/change-ws.rb, lib/rexml/validation/relaxng.rb,
  lib/yaml/baseemitter.rb: indentation fix.

* lib/matrix.rb: small cosmetic change.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20859 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-18 07:54:50 +00:00
knu 1ed799937b * enumerator.c (enumerator_next): Fix a typo: s/rewinded/rewound/.
* lib/prime.rb (Prime::OldCompatibility#each): Ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20604 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-10 03:49:48 +00:00
yugui 645f25356a * lib/prime.rb (Prime::OldCompatibility#each): added compatibility to
Ruby 1.8.7.
  (Prime#each): added more rdocs.
  (Prime#each): remembers the last value of the given block.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19135 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-04 14:09:52 +00:00
yugui fce093432e * lib/mathn.rb (Integer): moved into prime.rb.
(Prime): ditto.

* lib/prime.rb (Integer): moved from mathn.rb.
  (Integer.each_prime): added.
  (Integer#prime?): added.
  (Prime): moved from mathn.rb.
    Its implmentation was rewritten. see [ruby-dev:35863].
    And patched by Keiju ISHITSUKA <keiju@ishitsuka.com>,
    see [ruby-dev:36128].
  (Prime.new):                     obsolete.
  (Prime.instance):                added.
  (Prime.each):                    added.
  (Prime.int_from_prime_division): added.
  (Prime.prime_division):          added.
  (Prime.prime?):                  added.
    Patch by TOYOFUKU Chikanobu
    <nobu_toyofuku at nifty.com> in [ruby-dev:36067].
  (Prime.cache):                   removed.
  (Prime.primes):                  removed.
  (Prime.primes_so_far):           removed.
  (Prime#int_from_prime_division): added.
  (Prime#prime_division):          added.
  (Prime#prime?):                  added.
  (Prime#primes):                  removed.
  (Prime#primes_so_far):           removed.
  (Prime::PseudoPrmeGenerator):    added.
  (Prime::EratosthenesGenerator):  added.
  (Prime::TrialDivisionGenerator): added.
  (Prime::Generator23):            added.
  (Prime::TrialDivision):          added.
    Extracted from the previous implementation of Prime
    by Keiju ISHITSUKA.
  (Prime::EratosthenesSieve):      added.

* lib/.document (prime.rb): added

* lib/README (prime.rb): added

* test/test_prime.rb: added.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19095 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-03 13:57:21 +00:00