primes-utils 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/README.md +106 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/primes/utils.rb +244 -0
- data/lib/primes/utils/version.rb +5 -0
- data/primes-utils.gemspec +27 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 02ae799a48a427c43b317bd72d0c610fb2fd61c2
|
4
|
+
data.tar.gz: bc2dae709f9f666b50a83c0fe70c25d9f1d5251c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 07f57ffe182198ec1f90d318586edfda8b10371e5f0a74a3a7303dcb4c224f716145863ef1ede2b69a36c0402df4b5f9f4dbc64bcc98191d916a3e22fce2b4c3
|
7
|
+
data.tar.gz: f078d942fa9ebb6b8f98b4b2343f27fac5f89e43241989ff7ecd0a04b66561ecb15daf7c330d2191ce63d1f86d048ecc365d2e3e4c6e96a886755debf4bd0084
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# primes-utils
|
2
|
+
|
3
|
+
primes-utils is a Rubygem which provides a suite of extremely fast (relative to Ruby's standard library) utility methods for testing and generating primes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'primes-utils'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install primes-utils
|
20
|
+
|
21
|
+
## Methods
|
22
|
+
|
23
|
+
**prime?**
|
24
|
+
Determine if the absolute value of an integer is prime. Return 'true' or 'false'.
|
25
|
+
This replaces the `prime?` method in the `prime.rb` standard library.
|
26
|
+
```
|
27
|
+
101.prime? => true
|
28
|
+
100.prime? => false
|
29
|
+
-71.prime? => true
|
30
|
+
```
|
31
|
+
**primemr?(k=20)**
|
32
|
+
Determine if the absolute value of an integer is prime using Miller-Rabin test. Return 'true' or 'false'.
|
33
|
+
Miller-Rabin here is super fast, but probabilistic (not deterministic), primality test.
|
34
|
+
https://en.wikipedia.org/wiki/Miller-Rabin_primality_test
|
35
|
+
The method's reliability can be increased by increasing the default input parameter of k=20.
|
36
|
+
```
|
37
|
+
1111111111111111111.primemr? => true
|
38
|
+
1111111111111111111.primemr? 50 => true
|
39
|
+
1111111111111111111.primemr?(50) => true
|
40
|
+
11111111111111111111.primemr? => false
|
41
|
+
-3333333333333333333.primemr? => false
|
42
|
+
```
|
43
|
+
**factors(p=13) or prime_division(p=13)**
|
44
|
+
Determine the prime factorization of the absolute value of an integer.
|
45
|
+
This replaces the `prime division` method in the `prime.rb` standard library.
|
46
|
+
Returns an array of arrays of factors and exponents: [[2,4],[3,2],[5,1]] => (2^4)(3^2)(5^1) = (16)(9)(5) = 720
|
47
|
+
Default Strictly Prime (SP) Prime Generator (PG) used here is P13.
|
48
|
+
Can change SP PG used on input. Acceptable range are primes from 3 - 19.
|
49
|
+
```
|
50
|
+
1111111111111111111.prime_division => [[1111111111111111111, 1]]
|
51
|
+
11111111111111111111.prime_division => [[11, 1], [41, 1], [101, 1], [271, 1], [3541, 1], [9091, 1], [27961, 1]]
|
52
|
+
123456789.factors => [[3, 2], [3607, 1], [3803, 1]]
|
53
|
+
123456789.factors 17 => [[3, 2], [3607, 1], [3803, 1]]
|
54
|
+
123456789.factors(17) => [[3, 2], [3607, 1], [3803, 1]]
|
55
|
+
-12345678.factors => [[2, 1], [3, 2], [47, 1], [14593, 1]]
|
56
|
+
```
|
57
|
+
**primes(start_num=0)**
|
58
|
+
Create an array of primes from the range start_num - end_num.
|
59
|
+
The order of the range doesn't matter if both given: start.primes end <=> end.prime start
|
60
|
+
If only one parameter used, then all the primes upto that number will be returned.
|
61
|
+
```
|
62
|
+
50.primes => [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
|
63
|
+
50.primes 125 => [53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113]
|
64
|
+
300.primes 250 => [251, 257, 263, 269, 271, 277, 281, 283, 293]
|
65
|
+
541.primes.size => 100
|
66
|
+
1000.primes(5000).size => 501
|
67
|
+
(prms = 1000000.primes(1000100)).size => 6
|
68
|
+
prms.size => 6
|
69
|
+
prms => [1000003, 1000033, 1000037, 1000039, 1000081, 1000099]
|
70
|
+
-10.primes -50 => [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
|
71
|
+
```
|
72
|
+
**primenth(p=11) or nthprime(p=11)**
|
73
|
+
Return the value of the nth (absolute value) prime.
|
74
|
+
Default Strictly Prime (SP) Prime Generator (PG) used here is P11.
|
75
|
+
Can change SP PG used on input. Acceptable range are primes from 3 - 19.
|
76
|
+
```
|
77
|
+
1000000.primenth => 15485863
|
78
|
+
1500000.nthprime => 23879519
|
79
|
+
2000000.nthprime 13 => 32452843
|
80
|
+
-500000.nthprime => 7368787
|
81
|
+
```
|
82
|
+
## Coding Implementations
|
83
|
+
The methods `primemr`, `nthprime/primenth`, and `primes` are coded in pure ruby.
|
84
|
+
The methods `prime?` and `prime_division/factors` have two implementations.
|
85
|
+
Each has a pure ruby implementation, and also a hybrid implementation which uses the
|
86
|
+
Unix cli command `factor` if its available on the host OS. `factor` [5] is an extremely fast
|
87
|
+
C coded factoring algorithm, part of the GNU Core Utilities package [4].
|
88
|
+
|
89
|
+
Upon loading, the gem tests if the host `RUBY_PLATFORM` value is contained in a list
|
90
|
+
of known OSes which has the `factor` command. If so, it wraps a system call to `factor` and uses
|
91
|
+
it for `prime?` and `prime_division/factors`. If not, it uses a fast pure ruby implementation for
|
92
|
+
each method based on the Sieve of Zakiya (SoZ)[1][2][3]. OSes not on the list that have `factor`
|
93
|
+
can be added to the `factor_platforms` array to accommodate them.
|
94
|
+
|
95
|
+
## Author
|
96
|
+
Jabari Zakiya
|
97
|
+
|
98
|
+
## References
|
99
|
+
[1]https://www.scribd.com/doc/150217723/Improved-Primality-Testing-and-Factorization-in-Ruby-revised
|
100
|
+
[2]https://www.scribd.com/doc/228155369/The-Segmented-Sieve-of-Zakiya-SSoZ
|
101
|
+
[3]https://www.scribd.com/doc/73385696/The-Sieve-of-Zakiya
|
102
|
+
[4]https://en.wikipedia.org/wiki/GNU_Core_Utilities
|
103
|
+
[5]https://en.wikipedia.org/wiki/Factor_(Unix)
|
104
|
+
|
105
|
+
## License
|
106
|
+
GPL 2.0 or later.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "primes/utils"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/primes/utils.rb
ADDED
@@ -0,0 +1,244 @@
|
|
1
|
+
require "primes/utils/version"
|
2
|
+
|
3
|
+
module Primes
|
4
|
+
module Utils
|
5
|
+
private
|
6
|
+
factor_platforms = %w/linux bsd unix/
|
7
|
+
os_has_factor = factor_platforms.any? {|p| RUBY_PLATFORM.match(p)}
|
8
|
+
|
9
|
+
public
|
10
|
+
if os_has_factor # for platforms with cli 'factor' command
|
11
|
+
|
12
|
+
def prime?
|
13
|
+
`factor #{self.abs}`.split(' ').size == 2
|
14
|
+
end
|
15
|
+
|
16
|
+
def factors(p=0) # p is unused dummy variable for method consistency
|
17
|
+
factors = `factor #{self.abs}`.split(' ')[1..-1].map {|i| i.to_i}
|
18
|
+
h = Hash.new(0); factors.each {|f| h[f] +=1}; h.to_a.sort
|
19
|
+
end
|
20
|
+
|
21
|
+
puts "Using cli 'factor' for prime?/factors/prime_division"
|
22
|
+
|
23
|
+
else # use pure ruby versions for platforms without cli command 'factor'
|
24
|
+
|
25
|
+
def prime?
|
26
|
+
residues = [1,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,
|
27
|
+
89,97,101,103,107,109,113,121,127,131,137,139,143,149,151,157,163,
|
28
|
+
167,169,173,179,181,187,191,193,197,199,209,211]
|
29
|
+
mod=210; rescnt=48
|
30
|
+
|
31
|
+
n = self.abs
|
32
|
+
return true if [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
|
33
|
+
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103,
|
34
|
+
107, 109, 113, 127, 131, 137, 139, 149, 151, 157,163,
|
35
|
+
167, 173, 179, 181, 191, 193, 197, 199, 211].include? n
|
36
|
+
return false if not residues.include?(n%mod) || n == 1
|
37
|
+
|
38
|
+
sqrtN = Math.sqrt(n).to_i
|
39
|
+
p=11 # first test prime pj
|
40
|
+
while p <= sqrtN
|
41
|
+
return false if
|
42
|
+
n%(p) == 0 or n%(p+2) ==0 or n%(p+6) == 0 or n%(p+8) ==0 or
|
43
|
+
n%(p+12) == 0 or n%(p+18) ==0 or n%(p+20) == 0 or n%(p+26) ==0 or
|
44
|
+
n%(p+30) == 0 or n%(p+32) ==0 or n%(p+36) == 0 or n%(p+42) ==0 or
|
45
|
+
n%(p+48) == 0 or n%(p+50) ==0 or n%(p+56) == 0 or n%(p+60) ==0 or
|
46
|
+
n%(p+62) == 0 or n%(p+68) ==0 or n%(p+72) == 0 or n%(p+78) ==0 or
|
47
|
+
n%(p+86) == 0 or n%(p+90) ==0 or n%(p+92) == 0 or n%(p+96) ==0 or
|
48
|
+
n%(p+98) == 0 or n%(p+102)==0 or n%(p+110)== 0 or n%(p+116)==0 or
|
49
|
+
n%(p+120)== 0 or n%(p+126)==0 or n%(p+128)== 0 or n%(p+132)==0 or
|
50
|
+
n%(p+138)== 0 or n%(p+140)==0 or n%(p+146)== 0 or n%(p+152)==0 or
|
51
|
+
n%(p+156)== 0 or n%(p+158)==0 or n%(p+162)== 0 or n%(p+168)==0 or
|
52
|
+
n%(p+170)== 0 or n%(p+176)==0 or n%(p+180)== 0 or n%(p+182)==0 or
|
53
|
+
n%(p+186)== 0 or n%(p+188)==0 or n%(p+198)== 0 or n%(p+200)==0
|
54
|
+
p += mod # first prime candidate for next kth residues group
|
55
|
+
end
|
56
|
+
return true
|
57
|
+
end
|
58
|
+
|
59
|
+
def factors(p=13) # P13 is default prime generator here
|
60
|
+
seeds = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]
|
61
|
+
return 'PRIME OPTION NOT A SEEDS PRIME' if !seeds.include? p
|
62
|
+
|
63
|
+
# find primes <= Pn, compute modPn then Prime Gen residues for Pn
|
64
|
+
primes = seeds[0..seeds.index(p)]; mod = primes.reduce(:*)
|
65
|
+
residues=[1]; 3.step(mod,2) {|i| residues << i if mod.gcd(i) == 1}
|
66
|
+
residues << mod+1; rescnt = residues.size-1
|
67
|
+
|
68
|
+
n = self.abs
|
69
|
+
factors = []
|
70
|
+
|
71
|
+
return [] if n == 0
|
72
|
+
return [[n,1]] if primes.include? n
|
73
|
+
primes.each {|p| while n%p ==0; factors << p; n /= p end }
|
74
|
+
|
75
|
+
sqrtN= Math.sqrt(n).to_i
|
76
|
+
modk,r=0,1; p=residues[1] # first test prime pj
|
77
|
+
while p <= sqrtN
|
78
|
+
if n%p == 0
|
79
|
+
factors << p; r -=1; n /= p; sqrtN = Math.sqrt(n).to_i
|
80
|
+
end
|
81
|
+
r +=1; if r > rescnt; r=1; modk +=mod end
|
82
|
+
p = modk+residues[r] # next (or current) prime candidate
|
83
|
+
end
|
84
|
+
factors << n if n > 1
|
85
|
+
h=Hash.new(0); factors.each {|f| h[f] +=1}; h.to_a.sort
|
86
|
+
end
|
87
|
+
|
88
|
+
puts "Using pure ruby versions"
|
89
|
+
end
|
90
|
+
|
91
|
+
# Replace slow ruby library method prime_division with faster version
|
92
|
+
alias prime_division factors
|
93
|
+
|
94
|
+
def primenth(p=11) # return value of nth prime
|
95
|
+
# Return value of nth prime
|
96
|
+
# Uses sozP11 Sieve of Zakiya (SoZ) as default prime sieve
|
97
|
+
|
98
|
+
seeds = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]
|
99
|
+
primes = []
|
100
|
+
|
101
|
+
n = self.abs # the desired nth prime
|
102
|
+
return seeds[n-1] if n < 14
|
103
|
+
numb = 22*n # approx value need to check primes up to
|
104
|
+
|
105
|
+
# find primes <= Pn, compute modPn then Prime Gen residues for Pn
|
106
|
+
primes = seeds[0..seeds.index(p)]; mod = primes.reduce(:*)
|
107
|
+
residues=[1]; 3.step(mod,2) {|i| residues << i if mod.gcd(i) == 1}
|
108
|
+
residues << mod+1; rescnt = residues.size-1
|
109
|
+
|
110
|
+
num = numb-1 | 1 # if N even number then subtract 1
|
111
|
+
k=num/mod; modk = mod*k; r=1 # kth residue group; base num value
|
112
|
+
while num >= modk+residues[r]; r += 1 end # compute extra prms size
|
113
|
+
maxprms = k*rescnt + r-1 # max size of prime candidates array
|
114
|
+
prms=Array.new(maxprms,true) # set all prime candidates to True
|
115
|
+
|
116
|
+
# array of residues offsets to compute nonprimes positions in prms
|
117
|
+
pos =[]; rescnt.times {|i| pos[residues[i]] = i-1};
|
118
|
+
|
119
|
+
# sieve (SoZ) to eliminate nonprimes from prms
|
120
|
+
sqrtN = Math.sqrt(num).to_i
|
121
|
+
modk,r,k=0,0,0
|
122
|
+
prms.each do |prime|
|
123
|
+
r +=1; if r > rescnt; r=1; modk +=mod; k +=1 end
|
124
|
+
next unless prime
|
125
|
+
res_r = residues[r]
|
126
|
+
prime = modk + res_r
|
127
|
+
break if prime > sqrtN
|
128
|
+
prmstep = prime * rescnt
|
129
|
+
residues[1..-1].each do |ri|
|
130
|
+
# compute (prime * (modk + ri)) position index in prms
|
131
|
+
kk,rr = (res_r * ri).divmod mod # residues product res[group|track]
|
132
|
+
nonprm = (k*(prime + ri) + kk)*rescnt + pos[rr]
|
133
|
+
while nonprm < maxprms; prms[nonprm]=nil; nonprm +=prmstep end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
# the prms array now has all the primes positions for primes r1..N
|
137
|
+
# find the nth prime and output it
|
138
|
+
count = primes.size
|
139
|
+
modk,r=0,0
|
140
|
+
prms.each do |prime|
|
141
|
+
r +=1; if r > rescnt; r=1; modk +=mod end
|
142
|
+
count +=1 if prime
|
143
|
+
return modk+residues[r] if count == n
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
alias nthprime primenth # to make life easier
|
148
|
+
|
149
|
+
def primes(start_num=0)
|
150
|
+
# Find primes between a number range: end_num - start_num
|
151
|
+
# Use as: end_num.primes(start_num) or end_num.primes
|
152
|
+
# If start_num omitted, will find all primes <= end_num
|
153
|
+
# If start_num > self, values are switched to continue
|
154
|
+
# Output is an array of the primes within the given range
|
155
|
+
# To find count of these primes do: end_num.primes(start_num).size
|
156
|
+
# This method uses the P13 Strictly Prime (SP) Prime Generator
|
157
|
+
|
158
|
+
num = self.abs; start_num = start_num.abs
|
159
|
+
num, start_num = start_num, num if start_num > num
|
160
|
+
|
161
|
+
primes = [2,3,5,7,11,13] # P13 excluded primes lists
|
162
|
+
|
163
|
+
return primes.select {|p| p >= start_num && p <= num} if num <= 13
|
164
|
+
|
165
|
+
mod = 30030 # P13 modulus value 2*3*5*7*11*13
|
166
|
+
|
167
|
+
residues=[1]; 17.step(mod,2) {|i| residues << i if mod.gcd(i) == 1}
|
168
|
+
rescnt = residues.size # number of residues
|
169
|
+
residues << mod+1 # to make algorithm easier
|
170
|
+
|
171
|
+
num = num-1 | 1 # if N even number then subtract 1
|
172
|
+
k=num/mod; modk = mod*k; r=1 # kth residue group; base num value
|
173
|
+
while num >= modk+residues[r]; r += 1 end # compute extra prms size
|
174
|
+
maxprms = k*rescnt + r-1 # max size of prime candidates array
|
175
|
+
prms=Array.new(maxprms,true) # set all prime candidates to True
|
176
|
+
|
177
|
+
# hash of residues offsets to compute nonprimes positions in prms
|
178
|
+
pos =[]; rescnt.times {|i| pos[residues[i]] = i-1};
|
179
|
+
|
180
|
+
# sieve (SoZ) to eliminate nonprimes from prms
|
181
|
+
sqrtN= Math.sqrt(num).to_i
|
182
|
+
modk,r,k=0,0,0
|
183
|
+
prms.each do |prime|
|
184
|
+
r +=1; if r > rescnt; r=1; modk +=mod; k +=1 end
|
185
|
+
next unless prime
|
186
|
+
res_r = residues[r]
|
187
|
+
prime = modk + res_r
|
188
|
+
break if prime > sqrtN
|
189
|
+
prmstep = prime * rescnt
|
190
|
+
residues[1..-1].each do |ri|
|
191
|
+
# compute (prime * (modk + ri)) position index in prms
|
192
|
+
kk,rr = (res_r * ri).divmod mod # residues product res[group|track]
|
193
|
+
nonprm = (k*(prime + ri) + kk)*rescnt + pos[rr] # 1st prime multiple
|
194
|
+
while nonprm < maxprms; prms[nonprm]=nil; nonprm +=prmstep end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
# the prms array now has all the primes positions for primes r1..N
|
198
|
+
primes = start_num <= 13 ? primes.drop_while {|p| p < start_num} : []
|
199
|
+
k = start_num / mod
|
200
|
+
modk = mod*k
|
201
|
+
m = rescnt*k
|
202
|
+
r = 0
|
203
|
+
while m < maxprms
|
204
|
+
r +=1; if r > rescnt; r=1; modk +=mod end
|
205
|
+
if prms[m]
|
206
|
+
prime = modk + residues[r]
|
207
|
+
primes << prime if prime >= start_num
|
208
|
+
end
|
209
|
+
m +=1
|
210
|
+
end
|
211
|
+
primes
|
212
|
+
end
|
213
|
+
|
214
|
+
# Miller-Rabin prime test in Ruby
|
215
|
+
# From: http://en.wikipedia.org/wiki/Miller-Rabin_primality_test
|
216
|
+
# Ruby Rosetta Code: http://rosettacode.org/wiki/Miller-Rabin_primality_test
|
217
|
+
# I modified the Rosetta Code, as shown below
|
218
|
+
|
219
|
+
require 'openssl'
|
220
|
+
def primemr?(k=20) # increase k for more reliability
|
221
|
+
n = self.abs
|
222
|
+
return true if n == 2 or n == 3
|
223
|
+
return false if n % 6 != 1 && n % 6 != 5 or n == 1
|
224
|
+
|
225
|
+
d = n - 1
|
226
|
+
s = 0
|
227
|
+
(d >>= 1; s += 1) while d.even?
|
228
|
+
k.times do
|
229
|
+
a = 2 + rand(n-4)
|
230
|
+
x = a.to_bn.mod_exp(d,n) #x = (a**d) mod n
|
231
|
+
next if x == 1 or x == n-1
|
232
|
+
(s-1).times do
|
233
|
+
x = x.mod_exp(2,n) #x = (x**2) mod n
|
234
|
+
return false if x == 1
|
235
|
+
break if x == n-1
|
236
|
+
end
|
237
|
+
return false if x != n-1
|
238
|
+
end
|
239
|
+
true # with high probability
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
class Integer; include Primes end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'primes/utils/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "primes-utils"
|
8
|
+
spec.version = Primes::Utils::VERSION
|
9
|
+
spec.authors = ["Jabari Zakiya"]
|
10
|
+
spec.email = ["jzakiya@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{suite of extremely fast utility methods for testing and generating primes}
|
13
|
+
spec.description = %q{Methods: prime?, primemr?, primes, nthprime/primenth, factors/prime_division}
|
14
|
+
spec.homepage = "https://github.com/jzakiya/primes-utils"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
#if spec.respond_to?(:metadata)
|
22
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
|
23
|
+
#end
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: primes-utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jabari Zakiya
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-01 00:00:00.000000000 Z
|
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.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
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'
|
41
|
+
description: 'Methods: prime?, primemr?, primes, nthprime/primenth, factors/prime_division'
|
42
|
+
email:
|
43
|
+
- jzakiya@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- CODE_OF_CONDUCT.md
|
50
|
+
- Gemfile
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- bin/console
|
54
|
+
- bin/setup
|
55
|
+
- lib/primes/utils.rb
|
56
|
+
- lib/primes/utils/version.rb
|
57
|
+
- primes-utils.gemspec
|
58
|
+
homepage: https://github.com/jzakiya/primes-utils
|
59
|
+
licenses: []
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.4.6
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: suite of extremely fast utility methods for testing and generating primes
|
81
|
+
test_files: []
|
82
|
+
has_rdoc:
|