faster_prime 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +6 -2
- data/README.md +2 -0
- data/faster_prime.gemspec +1 -6
- data/lib/faster_prime.rb +1 -44
- data/lib/faster_prime/base.rb +44 -0
- data/lib/faster_prime/primality_test.rb +3 -3
- data/lib/faster_prime/version.rb +3 -0
- metadata +6 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99c137ff79c15c9efcbbe0fcf00075779cb4f62ae6f817f37ff099c4c429856c
|
4
|
+
data.tar.gz: 0ca0346315832936fcf957c95cd75f4160854bd3f03e1ddc31a4d2a07cc7aa42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 877cf77db068d4a0b2b77757f7666f78c08486a94482a618a29e57ff379cdfa91575eee84df2971d6d421746d789a2737ab97e0d9a5547f87643a4a622d85df4
|
7
|
+
data.tar.gz: cb68bf65604930530b44480025078baf5992dc027a0135fa02e423001647d2218cfbbf5344ce50f8651f8dbd09619191e72fbef72e1c055665396a0e20bb99aa
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -8,6 +8,8 @@ This is an alternative implementation to the standard `lib/prime.rb`. It is (al
|
|
8
8
|
* Replace `require "prime"` with `require "faster_prime"`.
|
9
9
|
* Replace `Prime` with `FasterPrime`.
|
10
10
|
|
11
|
+
If you want to use this without the core extension, you can use `require "faster_prime/base"` and `FasterPrime.prime?(num)` instead of `Integer#prime?`.
|
12
|
+
|
11
13
|
## Benchmark
|
12
14
|
|
13
15
|
### `Integer#prime?`
|
data/faster_prime.gemspec
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
|
2
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
-
require "faster_prime"
|
1
|
+
require_relative "lib/faster_prime/version"
|
4
2
|
|
5
3
|
Gem::Specification.new do |spec|
|
6
4
|
spec.name = "faster_prime"
|
@@ -18,7 +16,4 @@ Gem::Specification.new do |spec|
|
|
18
16
|
spec.bindir = "exe"
|
19
17
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
18
|
spec.require_paths = ["lib"]
|
21
|
-
|
22
|
-
spec.add_development_dependency "bundler", "~> 1.16"
|
23
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
24
19
|
end
|
data/lib/faster_prime.rb
CHANGED
@@ -1,45 +1,2 @@
|
|
1
|
-
require "faster_prime/
|
2
|
-
require "faster_prime/primality_test"
|
3
|
-
require "faster_prime/prime_factorization"
|
4
|
-
require "faster_prime/sieve"
|
1
|
+
require "faster_prime/base"
|
5
2
|
require "faster_prime/core_ext"
|
6
|
-
|
7
|
-
module FasterPrime
|
8
|
-
VERSION = "1.0.0"
|
9
|
-
|
10
|
-
module Core
|
11
|
-
include Enumerable
|
12
|
-
|
13
|
-
class SuccEnumerator < Enumerator
|
14
|
-
alias succ next
|
15
|
-
end
|
16
|
-
|
17
|
-
def each(ubound = nil, &block)
|
18
|
-
return SuccEnumerator.new(Float::INFINITY) do |y|
|
19
|
-
Sieve.each(ubound) {|p| y << p }
|
20
|
-
end unless block_given?
|
21
|
-
|
22
|
-
Sieve.each(ubound, &block)
|
23
|
-
end
|
24
|
-
|
25
|
-
def prime?(n)
|
26
|
-
raise ArgumentError, "Expected an integer, got #{ n }" unless n.respond_to?(:integer?) && n.integer?
|
27
|
-
PrimalityTest.prime?(n)
|
28
|
-
end
|
29
|
-
|
30
|
-
def prime_division(n)
|
31
|
-
PrimeFactorization.prime_factorization(n).to_a
|
32
|
-
end
|
33
|
-
|
34
|
-
def int_from_prime_division(pd)
|
35
|
-
pd.inject(1) {|v, (p, e)| v * (p ** e) }
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def self.instance
|
40
|
-
self
|
41
|
-
end
|
42
|
-
|
43
|
-
include Core
|
44
|
-
extend Core
|
45
|
-
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "faster_prime/version"
|
2
|
+
require "faster_prime/utils"
|
3
|
+
require "faster_prime/primality_test"
|
4
|
+
require "faster_prime/prime_factorization"
|
5
|
+
require "faster_prime/sieve"
|
6
|
+
require "faster_prime/core_ext"
|
7
|
+
|
8
|
+
module FasterPrime
|
9
|
+
module Core
|
10
|
+
include Enumerable
|
11
|
+
|
12
|
+
class SuccEnumerator < Enumerator
|
13
|
+
alias succ next
|
14
|
+
end
|
15
|
+
|
16
|
+
def each(ubound = nil, &block)
|
17
|
+
return SuccEnumerator.new(Float::INFINITY) do |y|
|
18
|
+
Sieve.each(ubound) {|p| y << p }
|
19
|
+
end unless block_given?
|
20
|
+
|
21
|
+
Sieve.each(ubound, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
def prime?(n)
|
25
|
+
raise ArgumentError, "Expected an integer, got #{ n }" unless n.respond_to?(:integer?) && n.integer?
|
26
|
+
PrimalityTest.prime?(n)
|
27
|
+
end
|
28
|
+
|
29
|
+
def prime_division(n)
|
30
|
+
PrimeFactorization.prime_factorization(n).to_a
|
31
|
+
end
|
32
|
+
|
33
|
+
def int_from_prime_division(pd)
|
34
|
+
pd.inject(1) {|v, (p, e)| v * (p ** e) }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.instance
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
include Core
|
43
|
+
extend Core
|
44
|
+
end
|
@@ -75,18 +75,18 @@ module FasterPrime
|
|
75
75
|
#
|
76
76
|
# (1) simplest
|
77
77
|
#
|
78
|
-
#
|
78
|
+
# APRCL.prime?(N) #=> true or false
|
79
79
|
#
|
80
80
|
# (2) reuse-table
|
81
81
|
#
|
82
|
-
# aprcl =
|
82
|
+
# aprcl = APRCL.new(B)
|
83
83
|
# aprcl.prime?(N1) #=> true or false
|
84
84
|
# aprcl.prime?(N2) #=> true or false
|
85
85
|
# aprcl.bound #=> upper bound (>= B)
|
86
86
|
#
|
87
87
|
# (3) manual-t (for test or debug)
|
88
88
|
#
|
89
|
-
# aprcl =
|
89
|
+
# aprcl = APRCP.new(B)
|
90
90
|
# aprcl.set_t(t)
|
91
91
|
# aprcl.prime?(N) #=> true or false
|
92
92
|
|
metadata
CHANGED
@@ -1,43 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faster_prime
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yusuke Endoh
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: bundler
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.16'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '1.16'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '10.0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '10.0'
|
11
|
+
date: 2020-12-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
41
13
|
description: This provides `Integer#prime?`, `Integer#prime_division`, and `Prime#each`
|
42
14
|
that are almost compatible to and faster than `lib/prime.rb`.
|
43
15
|
email:
|
@@ -52,11 +24,13 @@ files:
|
|
52
24
|
- Rakefile
|
53
25
|
- faster_prime.gemspec
|
54
26
|
- lib/faster_prime.rb
|
27
|
+
- lib/faster_prime/base.rb
|
55
28
|
- lib/faster_prime/core_ext.rb
|
56
29
|
- lib/faster_prime/primality_test.rb
|
57
30
|
- lib/faster_prime/prime_factorization.rb
|
58
31
|
- lib/faster_prime/sieve.rb
|
59
32
|
- lib/faster_prime/utils.rb
|
33
|
+
- lib/faster_prime/version.rb
|
60
34
|
homepage: https://github.com/mame/faster_prime
|
61
35
|
licenses: []
|
62
36
|
metadata: {}
|
@@ -75,8 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
49
|
- !ruby/object:Gem::Version
|
76
50
|
version: '0'
|
77
51
|
requirements: []
|
78
|
-
|
79
|
-
rubygems_version: 2.7.3
|
52
|
+
rubygems_version: 3.1.4
|
80
53
|
signing_key:
|
81
54
|
specification_version: 4
|
82
55
|
summary: A faster substitute for `lib/prime.rb`.
|