primes-utils 2.7.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/README.md +104 -83
- data/lib/primes/utils/version.rb +1 -1
- data/lib/primes/utils.rb +423 -375
- data/{primes-utils.gemspec → primes-utils-3.0.0.gemspec} +6 -10
- metadata +29 -14
- data/.gitignore +0 -16
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0725fbc92f6ce6963c9c9bc4f80d717ce89a147113e68c755b0c89a95eefd65a
|
|
4
|
+
data.tar.gz: 61569c65f7555a235a5999e5581e2d4ae55871f82c3a630572da60b81e5a6a93
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2439002353876269bf640dc36ad7ccf98c57c7400e080c8eb37d617ee37830f5936f5bb995eb0be26129597f90e6b41e7579bca5c3feefec6fef65bbe7f8e394
|
|
7
|
+
data.tar.gz: dffc1d9ac67254c4d13aec57febc3ff0b67fa1762b2b99b6d83d46ebae12afd641b966e45e91fc688a36f9c901f5778b18ecb65b9b913fb3640b8869d8ab3b84
|
data/README.md
CHANGED
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
## Introduction
|
|
4
4
|
|
|
5
|
-
`primes-utils` is a Rubygem
|
|
5
|
+
`primes-utils` is a Rubygem providing a suite of extremely fast methods for testing and generating primes.
|
|
6
6
|
|
|
7
7
|
For details on the Math and Code used to implement them see:
|
|
8
8
|
|
|
9
9
|
`PRIMES-UTILS HANDBOOK`
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Available for `FREE` to read|download at:
|
|
12
|
+
|
|
13
|
+
https://www.academia.edu/19786419/PRIMES_UTILS_HANDBOOK
|
|
12
14
|
|
|
13
15
|
https://www.scribd.com/doc/266461408/Primes-Utils-Handbook
|
|
14
16
|
|
|
@@ -37,64 +39,62 @@ Then require as:
|
|
|
37
39
|
|
|
38
40
|
**prime?**
|
|
39
41
|
|
|
40
|
-
Determine if
|
|
42
|
+
Determine if an integer value is prime. Return 'true' or 'false'.
|
|
41
43
|
This replaces the `prime?` method in the `prime.rb` standard library.
|
|
44
|
+
Uses PGT residues tests, then Miller-Rabin test using `primemr?`.
|
|
42
45
|
|
|
43
46
|
```
|
|
44
47
|
101.prime? => true
|
|
45
48
|
100.prime? => false
|
|
46
|
-
-71.prime? =>
|
|
49
|
+
-71.prime? => false
|
|
47
50
|
0.prime? => false
|
|
48
51
|
1.prime? => false
|
|
49
52
|
```
|
|
50
53
|
|
|
51
|
-
**primemr?(k=
|
|
54
|
+
**primemr?(k=5)**
|
|
52
55
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
The reliability can be increased by increasing the default input parameter of k=20.
|
|
56
|
+
Optimized deterministic (over 64-bits) implementation of Miller-Rabin algorithm.
|
|
57
|
+
Default non-deterministic reliability set at k = 5, set higher if desired for very large numbers > 64-bts.
|
|
56
58
|
|
|
57
59
|
```
|
|
58
|
-
|
|
59
|
-
1111111111111111111.primemr? 50 => true
|
|
60
|
-
1111111111111111111.primemr?(50) => true
|
|
61
|
-
11111111111111111111.primemr? => false
|
|
62
|
-
-3333333333333333333.primemr? => false
|
|
63
|
-
n=10**1700; (n+469).primemr? => true
|
|
64
|
-
0.primemr? => false
|
|
65
|
-
1.primemr? => false
|
|
60
|
+
n.prime?(6)
|
|
66
61
|
```
|
|
67
62
|
|
|
68
|
-
**factors
|
|
63
|
+
**factors or prime_division**
|
|
69
64
|
|
|
70
|
-
Determine the prime factorization of
|
|
65
|
+
Determine the prime factorization of an +|- integer value.
|
|
66
|
+
Uses Unix coreutils function `factors` if available.
|
|
71
67
|
This replaces the `prime_division` method in the `prime.rb` standard library.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
Can change SP PG used on input. Acceptable primes range: [3 - 19].
|
|
68
|
+
Multiplying the factors back will produce original number.
|
|
69
|
+
Output is array of tuples of factors and exponents elements: [[p1, e1], [p2, e2],..,[pn, en]].
|
|
75
70
|
|
|
76
71
|
```
|
|
77
72
|
1111111111111111111.prime_division => [[1111111111111111111, 1]]
|
|
78
|
-
11111111111111111111.prime_division
|
|
73
|
+
11111111111111111111.prime_division => [[11, 1], [41, 1], [101, 1], [271, 1], [3541, 1], [9091, 1], [27961, 1]]
|
|
79
74
|
123456789.factors => [[3, 2], [3607, 1], [3803, 1]]
|
|
80
|
-
|
|
81
|
-
123456789.factors(17) => [[3, 2], [3607, 1], [3803, 1]]
|
|
82
|
-
-12345678.factors => [[2, 1], [3, 2], [47, 1], [14593, 1]]
|
|
75
|
+
-12345678.factors => [[-1, 1], [2, 1], [3, 2], [47, 1], [14593, 1]]
|
|
83
76
|
0.factors => []
|
|
84
77
|
1.factors => []
|
|
85
78
|
```
|
|
86
79
|
|
|
87
|
-
**
|
|
80
|
+
**factors1**
|
|
81
|
+
|
|
82
|
+
Pure Ruby version equivalent of `factor`.
|
|
83
|
+
Not as fast as `factor` for some values with multiple large prime factors.
|
|
84
|
+
Always available if OS doesn't have `factor`
|
|
88
85
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
86
|
+
**primes(start=0), primesmr(start=0)**
|
|
87
|
+
|
|
88
|
+
Return an array of prime values within the inclusive integers range `[start_num - end_num]`.
|
|
89
|
+
Input order doesn't matter if both given: `start_num.primes end_num <=> end_num.prime start_num`.
|
|
90
|
+
A single input is taken as `end_num`, and the primes <= to it are returned.
|
|
91
|
+
`primes` is generally faster, and uses SSoZ to compute the range primes.
|
|
92
|
+
`primesmr` is slower, but isn't memory limited, especially for very large numbers|ranges.
|
|
92
93
|
See `PRIMES-UTILS HANDBOOK` for details on best use practices.
|
|
93
94
|
Also see `Error Handling`.
|
|
94
95
|
|
|
95
96
|
```
|
|
96
97
|
50.primes => [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
|
|
97
|
-
50.primesf 125 => [53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113]
|
|
98
98
|
300.primes 250 => [251, 257, 263, 269, 271, 277, 281, 283, 293]
|
|
99
99
|
n=10**100; (n-250).primesmr(n+250) => []
|
|
100
100
|
541.primes.size => 100
|
|
@@ -102,19 +102,18 @@ n=10**100; (n-250).primesmr(n+250) => []
|
|
|
102
102
|
(prms = 1000000.primes(1000100)).size => 6
|
|
103
103
|
prms.size => 6
|
|
104
104
|
prms => [1000003, 1000033, 1000037, 1000039, 1000081, 1000099]
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
n=10**8; (25*n).primes -> ERROR3: not enough memory to store all primes in output array. => nil
|
|
109
|
-
0.primesf => []
|
|
110
|
-
1.primesmr => []
|
|
105
|
+
101.primes 101 => [101]
|
|
106
|
+
1.primes => []
|
|
107
|
+
0.primesmr => []
|
|
111
108
|
```
|
|
112
109
|
|
|
113
|
-
**primescnt(start=0),
|
|
110
|
+
**primescnt(start=0), primescntmr(start=0)**
|
|
114
111
|
|
|
115
|
-
Provide count of primes within the
|
|
116
|
-
|
|
117
|
-
|
|
112
|
+
Provide count of primes within the inclusive integers range `[start_num - end_num]`.
|
|
113
|
+
Input order doesn't matter if both given: `start_num.primes end_num <=> end_num.prime start_num`.
|
|
114
|
+
A single input is taken as `end_num`, and the primes count <= to it are returned.
|
|
115
|
+
`primescnt` is faster; uses SSoZ to identify|count primes from closest hashed value starting point.
|
|
116
|
+
`primescntmr` is slower, but isn't memory limited, especially for very large numbers|ranges.
|
|
118
117
|
See `PRIMES-UTILS HANDBOOK` for details on best use practices.
|
|
119
118
|
Also see `Error Handling`.
|
|
120
119
|
|
|
@@ -122,77 +121,99 @@ Also see `Error Handling`.
|
|
|
122
121
|
100001.primescnt => 9592
|
|
123
122
|
100002.primescnt => 9592
|
|
124
123
|
100003.primescnt => 9593
|
|
125
|
-
100000.
|
|
124
|
+
100000.primescntmr 100500 => 40
|
|
126
125
|
n=10**400; (n-500).primescntmr(n+500) => 1
|
|
127
|
-
-10.primescnt -50 => 11
|
|
128
|
-
n=10**20; n.primescnt n+n -> ERROR1: range size too big for available memory. => nil
|
|
129
|
-
n=10**20; n.primescnt 100 -> ERROR2: end_num too big for available memory. => nil
|
|
130
126
|
n=10**8; (25*n).primescnt => 121443371
|
|
131
|
-
0.
|
|
127
|
+
0.primescnt => 0
|
|
132
128
|
1.primescntmr => 0
|
|
133
129
|
```
|
|
134
130
|
|
|
135
|
-
**primenth(p=
|
|
131
|
+
**primenth(p=0) or nthprime(p=0)**
|
|
136
132
|
|
|
137
|
-
Return
|
|
133
|
+
Return value of the nth prime.
|
|
138
134
|
Default Strictly Prime (SP) Prime Generator (PG) is adaptively selected.
|
|
139
|
-
Can change SP PG used on input. Acceptable primes range: [
|
|
140
|
-
Indexed nth primes now upto
|
|
135
|
+
Can change SP PG used on input. Acceptable primes range: [5, 7].
|
|
136
|
+
Indexed nth primes now upto 7 billionth.
|
|
137
|
+
With 16GB mem can compute upto at least 35.7+ billionth prime (using `bitarray`).
|
|
138
|
+
Returns `nil` for negative nth inputs.
|
|
141
139
|
Also see `Error Handling`.
|
|
142
140
|
|
|
143
141
|
```
|
|
144
142
|
1000000.primenth => 15485863
|
|
145
143
|
1500000.nthprime => 23879519
|
|
146
144
|
2000000.nthprime 11 => 32452843
|
|
147
|
-
-500000.nthprime => 7368787
|
|
148
145
|
1122951705.nthprime => 25741879847
|
|
149
|
-
n = 10**11; n.primenth ->
|
|
150
|
-
|
|
151
|
-
|
|
146
|
+
n = 10**11; n.primenth -> #<NoMemoryError: failed to allocate memory>
|
|
147
|
+
2_123_409_000.nthprime => 50092535639
|
|
148
|
+
4_762_719_305.nthprime => 116378528093
|
|
149
|
+
0.nthprime => 0
|
|
150
|
+
1.primenth => 2
|
|
151
|
+
-1.nthprime => nil
|
|
152
|
+
```
|
|
153
|
+
**next_prime**
|
|
154
|
+
|
|
155
|
+
Return value of next prime > n. Returns `nil` for negative inputs.
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
100.next_prime => 101
|
|
159
|
+
101.next_prime => 103
|
|
160
|
+
0.next_prime => 2
|
|
161
|
+
-1.next_prime => nil
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
**prev_prime**
|
|
165
|
+
|
|
166
|
+
Return value of previous prime < n > 2. Returns `nil` for n < 2 (and negatives)
|
|
167
|
+
|
|
168
|
+
```
|
|
169
|
+
102.pref_prime => 101
|
|
170
|
+
101.prev_prime => 97
|
|
171
|
+
3.prev_prime => 2
|
|
172
|
+
2.prev_prime => nil
|
|
173
|
+
-1.prev_prime => nil
|
|
152
174
|
```
|
|
153
175
|
|
|
154
176
|
**primes_utils**
|
|
155
177
|
|
|
156
|
-
Displays a list of all the `primes-utils` methods available for
|
|
157
|
-
Use as `
|
|
178
|
+
Displays a list of all the `primes-utils` methods available for a system.
|
|
179
|
+
Use as eg: `0.primes_utils` where input n is any `class Integer` value.
|
|
180
|
+
|
|
181
|
+
Available methods for 3.0.0.
|
|
158
182
|
|
|
159
183
|
```
|
|
160
|
-
0.primes_utils => "prime?
|
|
184
|
+
0.primes_utils => "prime? primes primesmr primescnt primescntmr primenth|nthprime factors|prime_division factors1 next_prime prev_prime primes_utils"
|
|
161
185
|
```
|
|
162
186
|
|
|
163
187
|
## Error Handling
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
`nthprime|primenth` also displays the error message `<pcnt> not enough primes, approx nth too small.`
|
|
169
|
-
(`<pcnt>` is computed count of primes) when the computed approx_nth value is < nth value (though this should never happen by design).
|
|
170
|
-
With 2.4.0 error handling was added to `primes` that catches the error and displays message `ERROR3: not enough memory to store all primes in output array.`.
|
|
171
|
-
For all errors, the return value for each method is `nil`.
|
|
188
|
+
With 3.0.0 the Rubygem `bitarray` is used to extend useable memory for `primes`, `primescnt`, and `nthprime`.
|
|
189
|
+
They use private method `sozcores2` to compute the SoZ over the ranges, and returns an array of prime residues positions.
|
|
190
|
+
If an input range exceeds system memory to create the array, it switches to using a `bitarray`.
|
|
191
|
+
This greatly extends the computable range sizes, at the expense of being slower than using system memory.
|
|
172
192
|
|
|
173
193
|
There is also the rare possibility you could get a `NoMemoryError: failed to allocate memory` for the methods
|
|
174
|
-
`
|
|
194
|
+
`primes|primesmr` if their list of numerated primes is bigger than the amount of available system memory needed to store them.
|
|
175
195
|
If those methods are used as designed these errors won't occur, so the extra code isn't justified for them.
|
|
176
196
|
If they occur you will know why now.
|
|
177
197
|
|
|
178
198
|
This behavior is referenced to MRI Ruby.
|
|
179
199
|
|
|
180
200
|
## Coding Implementations
|
|
181
|
-
The
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
The methods `primesf` and `primescntf` use the `factor` version of `prime?` and are created if it exits.
|
|
185
|
-
`factor` [5] is an extremely fast C coded factoring algorithm, part of the GNU Core Utilities package [4].
|
|
201
|
+
The method `prime_division|factors` has 2 implementations. A pure ruby implementation, and a hybrid implementation
|
|
202
|
+
using the Unix cli command `factor` [5], if available on the host OS. It's an extremely fast C coded factoring algorithm,
|
|
203
|
+
part of the GNU Core Utilities package [4].
|
|
186
204
|
|
|
187
205
|
Upon loading, the gem tests if the command `factor` exists on the host OS.
|
|
188
|
-
If so, it performs a system call to it within `
|
|
206
|
+
If so, it performs a system call to it within `prime_division|factors`, and Ruby reformats its output.
|
|
189
207
|
If not, each method uses a fast pure ruby implementation based on the Sieve of Zakiya (SoZ)[1][2][3].
|
|
190
|
-
New in 2.2.0, upon loading with Ruby 1.8 `require 'rubygems'` is invoked to enable installing gems.
|
|
191
208
|
|
|
192
|
-
|
|
209
|
+
In 3.0.0, YJIT is enabled on install; and the methods coding is optimized for Ruby >= 3.3.
|
|
210
|
+
|
|
211
|
+
All the `primes-utils` methods are `instance_methods` for `Class Integer`.
|
|
193
212
|
|
|
194
213
|
## History
|
|
195
214
|
```
|
|
215
|
+
3.0.0 – YJIT enabled for Ruby >= 3.3, added new methods: next_prime, prev_prime.
|
|
216
|
+
Uses 'bitarray' to extend memory use for methods 'nthprime', 'primes', and 'primescnt'.
|
|
196
217
|
2.7.0 – more tweaking adaptive pg selection ranges in select_pg; coded using between? instead of cover?
|
|
197
218
|
2.6.0 – much, much better adaptive pg selection algorithm used in select_pg
|
|
198
219
|
2.5.1 – corrected minor error in select_pg
|
|
@@ -202,9 +223,9 @@ All the `primes-utils` methods are `instance_methods` for `class Integer`.
|
|
|
202
223
|
2.4.0 – fixed error in algorithm when ks resgroup ≤ sqrt(end_num) resgroup; algorithm now split
|
|
203
224
|
arrays when start_num > sqrt(end_num) in sozcore2, whose code also signficantly optimized,
|
|
204
225
|
with API change adding pcs2start value to output parameters to use in primenth, which changed
|
|
205
|
-
to use it; ruby idiom code opt for set_start_value; consolidated pcs_to_num | pcs_to_start_num
|
|
226
|
+
to use it; ruby idiom code opt for set_start_value; consolidated pcs_to_num | pcs_to_start_num
|
|
206
227
|
functions into one new pcs_to_num, with associated changes in sozcore1|2; primes|cnt also
|
|
207
|
-
significantly faster resulting from sozcore2 changes; massive code cleanups all-arround; added
|
|
228
|
+
significantly faster resulting from sozcore2 changes; massive code cleanups all-arround; added
|
|
208
229
|
private methods select_pg (to adaptively select the pg used in primes), and array_check (used in
|
|
209
230
|
sozcore2 to catch array creation out-of-memory errors)
|
|
210
231
|
2.3.0 – primescnt now finds primes upto some integer much faster, and for much larger integers
|
|
@@ -227,17 +248,17 @@ All the `primes-utils` methods are `instance_methods` for `class Integer`.
|
|
|
227
248
|
1.0.1 – check if using Ruby 1.8 at start, if so, require 'rational' library for gcd method
|
|
228
249
|
1.0.0 – initial release April 1, 2015 with methods prime?, primemr?, primes, prime_division|factors, primenth|nthprime
|
|
229
250
|
```
|
|
230
|
-
|
|
231
251
|
## Author
|
|
232
252
|
Jabari Zakiya
|
|
233
253
|
|
|
234
254
|
## References
|
|
235
|
-
[1]https://www.scribd.com/doc/150217723/Improved-Primality-Testing-and-Factorization-in-Ruby-revised
|
|
236
|
-
[2]https://www.scribd.com/doc/228155369/The-Segmented-Sieve-of-Zakiya-SSoZ
|
|
237
|
-
[3]https://www.scribd.com/doc/73385696/The-Sieve-of-Zakiya
|
|
238
|
-
[4]https://en.wikipedia.org/wiki/GNU_Core_Utilities
|
|
239
|
-
[5]https://en.wikipedia.org/wiki/Factor_(Unix)
|
|
240
|
-
[6]https://en.wikipedia.org/wiki/Miller-Rabin_primality_test
|
|
255
|
+
[1] https://www.scribd.com/doc/150217723/Improved-Primality-Testing-and-Factorization-in-Ruby-revised
|
|
256
|
+
[2] https://www.scribd.com/doc/228155369/The-Segmented-Sieve-of-Zakiya-SSoZ
|
|
257
|
+
[3] https://www.scribd.com/doc/73385696/The-Sieve-of-Zakiya
|
|
258
|
+
[4] https://en.wikipedia.org/wiki/GNU_Core_Utilities
|
|
259
|
+
[5] https://en.wikipedia.org/wiki/Factor_(Unix)
|
|
260
|
+
[6] https://en.wikipedia.org/wiki/Miller-Rabin_primality_test
|
|
261
|
+
[7] https://www.academia.edu/105821370/Twin_Primes_Segmented_Sieve_of_Zakiya_SSoZ_Explained_Review_Article
|
|
241
262
|
|
|
242
263
|
## License
|
|
243
|
-
|
|
264
|
+
LGPL-2.0-or-later
|
data/lib/primes/utils/version.rb
CHANGED