primes-utils 3.1.1 → 3.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e4efbd1adfba3980cb7d23ff8d6a604c835a1d8159993d52676941534ac9454
4
- data.tar.gz: 1e730c6ec9e6e505cc7dbcaa706c7a788f25fd3b746b8d3738002f2c95d2d8bd
3
+ metadata.gz: c78862f12ef7e60a4d562eab42073cb8e9773b0cc995da54fb30e7e90a0471cd
4
+ data.tar.gz: 361d35630d1a050a44d181f649cc2dbc86d00de5a419baec98db9087c751e8ff
5
5
  SHA512:
6
- metadata.gz: 6af6def197418a3c5cf68c0a75d0ce182a96b60f1f414d6da2762ddf7156476a0d863f79bc65d08dc6824d62421f932e082ffb0b718f632a19ac416af618292a
7
- data.tar.gz: 954aa75ea469ef4ccaaaf3b14b20c78d4d2c1b7e2ee7803126d40e2d35fbcd68e9682bf30710ca7127af31b2f86688951970bef2f7e51ac8ff3102973d0ca984
6
+ metadata.gz: ca7e344e0400e4880df99fd6b0455e3763639e706c0e7aba36892328c203f06a638e349ec01884b7c1c51bbf95246ae3024a3af1f609500ef04850a89145a20d
7
+ data.tar.gz: '092ed83b8261aa9fe344fecb8fd0c41ac9755f0cb57f9f69e88bd017df8475ce6a70ddb5e0f3461b5ddedee6b1de858a09204a6b7446824cfc01fd627a3b0978'
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ .directory
12
+ *.odt
13
+ *.odt#
14
+ *.pdf
15
+ *test*.*
16
+ *.ods
data/README.md CHANGED
@@ -2,16 +2,21 @@
2
2
 
3
3
  ## Introduction
4
4
 
5
- `primes-utils` is a Rubygem providing a suite of extremely fast methods for testing and generating primes.
5
+ `primes-utils` is a Rubygem which provides a suite of extremely fast utility 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
- Available for `FREE` to read|download at:
11
+ Now available and `FREE` to view and download at:
12
12
 
13
13
  https://www.academia.edu/19786419/PRIMES_UTILS_HANDBOOK
14
14
 
15
+ or
16
+
17
+ https://www.scribd.com/doc/266461408/Primes-Utils-Handbook
18
+
19
+
15
20
  ## Installation
16
21
 
17
22
  Add this line to your application's Gemfile:
@@ -36,8 +41,8 @@ Then require as:
36
41
 
37
42
  **prime?**
38
43
 
39
- Determine if an integer value is prime, and return `true` or `false`.
40
- This replaces the `prime?` method in the `prime.rb` standard library.
44
+ Determine if the absolute value of an integer is prime. Return 'true' or 'false'.
45
+ This replaces the `prime?` method in the `prime.rb` standard library.
41
46
  Uses PGT residues tests, then Miller-Rabin test using `primemr?`.
42
47
 
43
48
  ```
@@ -50,77 +55,64 @@ Uses PGT residues tests, then Miller-Rabin test using `primemr?`.
50
55
 
51
56
  **primemr?(k=5)**
52
57
 
53
- Optimized deterministic (to 128-bits) implementation of Miller-Rabin algorithm.
54
- It's the underlying primality test for `prime`, and used in other methods.
55
- Default probabilistic reliability constant set at k = 5, set higher if desired for numbers > 128-bts.
56
- Not really necessary to do though.
57
-
58
- ```
59
- 5_000_000_000_000_000_003.primemr? => true
60
- 987_654_321_012_345_678_901_382_737.primemr? 6 => true
61
- 987_654_321_012_345_678_901_382_739.primemr? 6 => false
62
- ```
58
+ Optimized deterministic (over 64-bits) implementation of Miller-Rabin algorithm.
59
+ Set k > 5 for higher probability for very large numbers.
63
60
 
64
61
  **factors or prime_division**
65
62
 
66
- Determine the prime factorization of an +|- integer value.
67
- Uses Unix coreutils function `factor` if available.
68
- This replaces the `prime_division` method in the `prime.rb` standard library.
69
- Multiplying the factors back will produce original number.
70
- Output is array of tuples of factors and exponents elements: [[p1, e1], [p2, e2],..,[pn, en]].
63
+ Determine the prime factorization of the absolute value of an integer.
64
+ Uses Unix coreutils function `factors` if available.
65
+ This replaces the `prime_division` method in the `prime.rb` standard library.
66
+ Output is array of arrays of factors and exponents: [[p1,e1],[p2,e2]..[pn,en]].
71
67
 
72
68
  ```
73
69
  1111111111111111111.prime_division => [[1111111111111111111, 1]]
74
- 11111111111111111111.prime_division => [[11, 1], [41, 1], [101, 1], [271, 1], [3541, 1], [9091, 1], [27961, 1]]
70
+ 11111111111111111111.prime_division => [[11, 1], [41, 1], [101, 1], [271, 1], [3541, 1], [9091, 1], [27961, 1]]
75
71
  123456789.factors => [[3, 2], [3607, 1], [3803, 1]]
76
- -12345678.factors => [[-1, 1], [2, 1], [3, 2], [47, 1], [14593, 1]]
72
+ 123456789.factors 17 => [[3, 2], [3607, 1], [3803, 1]]
73
+ 123456789.factors(17) => [[3, 2], [3607, 1], [3803, 1]]
74
+ -12345678.factors => [[2, 1], [3, 2], [47, 1], [14593, 1]]
77
75
  0.factors => []
78
76
  1.factors => []
79
77
  ```
80
78
 
81
79
  **factors1**
82
80
 
83
- Pure Ruby version equivalent of `factors`.
84
- Not as fast as `factors` for some values with multiple large prime factors.
85
- Always available if OS doesn't have coreutils `factor`.
86
-
87
- ```
88
- 123_456_789.factors1 => [[3, 1], [7, 2], [839087, 1]]
89
- 5_000_000_000_000_000_003.factors1 => [[5000000000000000003, 1]]
90
- 987_654_321_012_345_678_901_382_739.factors1 => [[3, 1], [23, 1], [139, 1], [421, 1], [3469, 1], [7393, 1], [135899, 1], [70180703, 1]]
91
- ```
81
+ Pure Ruby version equivalent of `factor`.
82
+ Not as fast as `factor` for some large values with large prime factors.
83
+ Always available if OS doesn't have `factor`
92
84
 
93
85
  **primes(start=0), primesmr(start=0)**
94
86
 
95
- Return an array of prime values within the inclusive integers range `[start_num - end_num]`.
96
- Input order doesn't matter if both given: `start_num.primes end_num <=> end_num.prime start_num`.
97
- A single input is taken as `end_num`, and the primes <= to it are returned.
98
- `primes` is generally faster, and uses SoZ to compute the range primes.
99
- `primesmr` is slower, but isn't memory limited, especially for very large numbers|ranges.
100
- See `PRIMES-UTILS HANDBOOK` for details on best use practices.
87
+ Return an array of primes within the absolute value range `(|start| - |end|)`.
88
+ The order of the range doesn't matter if both given: `start.primes end <=> end.prime start`.
89
+ If only one parameter used, then all the primes up to that number will be returned.
90
+ See `PRIMES-UTILS HANDBOOK` for details on best use practices.
101
91
  Also see `Error Handling`.
102
92
 
103
93
  ```
104
94
  50.primes => [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
105
- 300.primesmr 250 => [251, 257, 263, 269, 271, 277, 281, 283, 293]
95
+ 50.primesf 125 => [53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113]
96
+ 300.primes 250 => [251, 257, 263, 269, 271, 277, 281, 283, 293]
106
97
  n=10**100; (n-250).primesmr(n+250) => []
107
98
  541.primes.size => 100
108
99
  1000.primes(5000).size => 501
109
100
  (prms = 1000000.primes(1000100)).size => 6
101
+ prms.size => 6
110
102
  prms => [1000003, 1000033, 1000037, 1000039, 1000081, 1000099]
111
- 101.primes 101 => [101]
112
- 1.primes => []
113
- 0.primesmr => []
103
+ -10.primes -50 => [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
104
+ n=10**20; n.primes n+n -> ERROR1: range size too big for available memory. => nil
105
+ n=10**20; n.primes 100 -> ERROR2: end_num too big for available memory. => nil
106
+ n=10**8; (25*n).primes -> ERROR3: not enough memory to store all primes in output array. => nil
107
+ 0.primesf => []
108
+ 1.primesmr => []
114
109
  ```
115
110
 
116
111
  **primescnt(start=0), primescntmr(start=0)**
117
112
 
118
- Provide count of primes within the inclusive integers range `[start_num - end_num]`.
119
- Input order doesn't matter if both given: `start_num.primes end_num <=> end_num.prime start_num`.
120
- A single input is taken as `end_num`, and the primes count <= to it are returned.
121
- `primescnt` is generally faster; uses SoZ to identify|count primes from closest hashed value starting point.
122
- `primescntmr` is situationally slower, but isn't memory limited, especially for very large numbers|ranges.
123
- Can also perform effective primality test on an integer n as: `n.primescntmr n`
113
+ Provide count of primes within the absolute value range `(|start| - |end|)`.
114
+ The order of the range doesn't matter if both given: `start.primes end <=> end.prime start`.
115
+ If only one parameter used, the count of all the primes up to that number will be returned.
124
116
  See `PRIMES-UTILS HANDBOOK` for details on best use practices.
125
117
  Also see `Error Handling`.
126
118
 
@@ -128,106 +120,103 @@ Also see `Error Handling`.
128
120
  100001.primescnt => 9592
129
121
  100002.primescnt => 9592
130
122
  100003.primescnt => 9593
131
- 100000.primescntmr 100500 => 40
123
+ 100000.primescntf 100500 => 40
132
124
  n=10**400; (n-500).primescntmr(n+500) => 1
125
+ -10.primescnt -50 => 11
126
+ n=10**20; n.primescnt n+n -> ERROR1: range size too big for available memory. => nil
127
+ n=10**20; n.primescnt 100 -> ERROR2: end_num too big for available memory. => nil
133
128
  n=10**8; (25*n).primescnt => 121443371
134
- 541.primescntmr 7919 => 901
135
- 0.primescnt => 0
129
+ 0.primescntf => 0
136
130
  1.primescntmr => 0
137
- 100.primescntmr 100 => 0
138
- 101.primescntmr 101 => 1
139
131
  ```
140
132
 
141
133
  **primenth(p=0) or nthprime(p=0)**
142
134
 
143
- Return value of the nth prime.
144
- Default Strictly Prime (SP) Prime Generator (PG) is adaptively selected.
145
- Can change SP PG used on input. Default is 7. (Usable are 5, 7, 11, but normally just use default.)
146
- Indexed nth primes now up to 7 billionth.
147
- With 16GB mem can compute up to about 30.7+ billionth prime (using `bitarray`).
148
- Returns `nil` for negative nth inputs. Also see `Error Handling`.
135
+ Return the value of the nth prime.
136
+ Default Strictly Prime (SP) Prime Generator (PG) is adaptively selected.
137
+ Can change SP PG used on input. Acceptable primes range: [5, 7].
138
+ Indexed nth primes now upto 2.01 billionth.
139
+ With 16GB mem can compute upto around 2.1+ billionth prime; higher with more mem.
140
+ Runtime error for negative nthprime values.
141
+ Also see `Error Handling`.
149
142
 
150
143
  ```
151
144
  1000000.primenth => 15485863
152
145
  1500000.nthprime => 23879519
153
- 2000000.nthprime(7) => 32452843
146
+ 2000000.nthprime 11 => 32452843
154
147
  1122951705.nthprime => 25741879847
155
- n = 10**11; n.primenth -> #<NoMemoryError: failed to allocate memory>
148
+ n = 10**11; n.primenth -> ERROR1: range size too big for available memory. => nil
149
+ 0.nthprime => 0
150
+ 1.primenth => 2
156
151
  2_123_409_000.nthprime => 50092535639
157
- 4_762_719_305.nthprime => 116378528093
158
- 7_123_456_789.nthprime => 177058934933
159
- 1.primenth => 2
160
- 0.nthprime => nil
161
- -1.nthprime => nil
162
152
  ```
153
+
163
154
  **next_prime**
164
155
 
165
- Return value of next prime > n. Returns `nil` for negative inputs.
156
+ Return value of next prime > n.
166
157
 
167
158
  ```
168
159
  100.next_prime => 101
169
160
  101.next_prime => 103
170
- 0.next_prime => 2
171
- -1.next_prime => nil
172
161
  ```
173
162
 
174
163
  **prev_prime**
175
164
 
176
- Return value of previous prime < n > 2. Returns `nil` for n < 2 (and negatives)
165
+ Return value of previous prime < n.
177
166
 
178
167
  ```
179
168
  102.pref_prime => 101
180
169
  101.prev_prime => 97
181
- 3.prev_prime => 2
182
- 2.prev_prime => nil
183
- -1.prev_prime => nil
170
+ 2.prev_prime => nil
184
171
  ```
185
172
 
186
173
  **primes_utils**
187
174
 
188
- Displays a list of all the `primes-utils` methods available for a system.
189
- Use as eg: `0.primes_utils` where input n is any `class Integer` value.
175
+ Displays a list of all the `primes-utils` methods available for your system.
176
+ Use as `n.primes_utils` where n is any `class Integer` value.
190
177
 
191
- Available methods for 3.1.1.
178
+ Available methods for 3.0.0.
192
179
 
193
180
  ```
194
181
  0.primes_utils => "prime? primes primesmr primescnt primescntmr primenth|nthprime factors|prime_division factors1 next_prime prev_prime primes_utils"
195
182
  ```
196
183
 
184
+
197
185
  ## Error Handling
198
- With 3.0.0 the Rubygem `bitarray` is used to extend useable memory for `primes`, `primescnt`, and `nthprime`.
199
- They use private method `sozcores2` to compute the SoZ over the ranges, and returns an array of prime residues positions.
200
- If an input range exceeds system memory to create the array, it switches to using a `bitarray`.
201
- This greatly extends the computable range sizes, at the expense of being slower than using system memory.
186
+ Starting with 2.2.0, error handling has been implemented to gracefully fail when array creation requires more memory than available.
187
+ This occurs when the range size, or end_num, need arrays greater than the amount of avalable memory. The first case shows the message
188
+ `ERROR1: range size too big for available memory.` and the second case `ERROR2: end_num too big for available memory.`
189
+ The affected methods are `primes`, `primescnt`, and `nthprime|primenth`.
190
+ `nthprime|primenth` also displays the error message `<pcnt> not enough primes, approx nth too small.`
191
+ (`<pcnt>` is computed count of primes) when the computed approx_nth value is < nth value (though this should never happen by design).
192
+ 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.`.
193
+ For all errors, the return value for each method is `nil`.
202
194
 
203
195
  There is also the rare possibility you could get a `NoMemoryError: failed to allocate memory` for the methods
204
- `primes|primesmr` if their list of numerated primes is bigger than the amount of available system memory needed to store them.
196
+ `primesf` and `primesmr` if their list of numerated primes is bigger than the amount of available system memory needed to store them.
205
197
  If those methods are used as designed these errors won't occur, so the extra code isn't justified for them.
206
198
  If they occur you will know why now.
207
199
 
208
200
  This behavior is referenced to MRI Ruby.
209
201
 
210
202
  ## Coding Implementations
211
- The method `prime_division|factors` has 2 implementations. A pure ruby implementation `factors1`, and a hybrid implementation
212
- using the Unix cli command `factor` [5], if available on the host OS. It's an extremely fast C coded factoring algorithm,
213
- part of the GNU Core Utilities package [4]. However, `factors1` is always exists separately.
203
+ The methods `prime?`, `nthprime|primenth`, `primes`, `primescnt`, `primesmr`, and `primescnt` are coded in pure ruby.
204
+ The methods `prime?` and `prime_division|factors` have two implementations.
205
+ Each has a pure ruby implementation, and a hybrid implementation using the Unix cli command `factor` if its available on the host OS.
206
+ The methods `primesf` and `primescntf` use the `factor` version of `prime?` and are created if it exits. (removed in >= 3.0.0)
207
+ `factor` [5] is an extremely fast C coded factoring algorithm, part of the GNU Core Utilities package [4].
214
208
 
215
209
  Upon loading, the gem tests if the command `factor` exists on the host OS.
216
- If so, it performs a system call to it within `prime_division|factors`, and Ruby reformats its output.
210
+ If so, it performs a system call to it within `prime?` and `prime_division|factors`, which uses its output.
217
211
  If not, each method uses a fast pure ruby implementation based on the Sieve of Zakiya (SoZ)[1][2][3].
212
+ New in 2.2.0, upon loading with Ruby 1.8 `require 'rubygems'` is invoked to enable installing gems.
213
+ New in 3.0.0, YJIT enabled on install; recoded for optimization using Ruby >= 3.3
218
214
 
219
- In 3.0.0, YJIT is enabled on install; and the methods coding is optimized for Ruby >= 3.3.
220
-
221
- All the `primes-utils` methods are `instance_methods` for `Class Integer`.
215
+ All the `primes-utils` methods are `instance_methods` for `class Integer`.
222
216
 
223
217
  ## History
224
218
  ```
225
- 3.1.1 – some methods refactoring|DRYing, documentation updates
226
- 3.1.0 – major performance enhancements for methods primescnt, primescntmr, and nthprimes,
227
- primarily by extending nth primes hash values up to 10.1 billion. Can now find nth primes
228
- over ranges up to 10.1 billion, and thus prime counts up to 253+ billion, in < 30 secs at ~5Ghz.
229
- 3.0.5 – YJIT enabled for Ruby >= 3.3, added new methods: next_prime, prev_prime.
230
- Uses 'bitarray' to extend memory use for methods 'nthprime', 'primes', and 'primescnt'.
219
+ 3.0.0 – YJIT enabled for Ruby >= 3.3, new methods: next_prime, prev_prime,
231
220
  2.7.0 – more tweaking adaptive pg selection ranges in select_pg; coded using between? instead of cover?
232
221
  2.6.0 – much, much better adaptive pg selection algorithm used in select_pg
233
222
  2.5.1 – corrected minor error in select_pg
@@ -242,7 +231,7 @@ All the `primes-utils` methods are `instance_methods` for `Class Integer`.
242
231
  significantly faster resulting from sozcore2 changes; massive code cleanups all-arround; added
243
232
  private methods select_pg (to adaptively select the pg used in primes), and array_check (used in
244
233
  sozcore2 to catch array creation out-of-memory errors)
245
- 2.3.0 – primescnt now finds primes up to some integer much faster, and for much larger integers
234
+ 2.3.0 – primescnt now finds primes upto some integer much faster, and for much larger integers
246
235
  increased index nth primes to over 2 billionth; used in nthprime|primenth and primescnt
247
236
  2.2.0 – for sozcore2: refactored to include more common code; changed output api; added memory
248
237
  error messages when prms and prms_range arrays creation fails; for primenth: used new
@@ -262,17 +251,24 @@ All the `primes-utils` methods are `instance_methods` for `Class Integer`.
262
251
  1.0.1 – check if using Ruby 1.8 at start, if so, require 'rational' library for gcd method
263
252
  1.0.0 – initial release April 1, 2015 with methods prime?, primemr?, primes, prime_division|factors, primenth|nthprime
264
253
  ```
254
+
265
255
  ## Author
266
256
  Jabari Zakiya
267
257
 
268
258
  ## References
269
- [1] https://www.scribd.com/doc/150217723/Improved-Primality-Testing-and-Factorization-in-Ruby-revised
270
- [2] https://www.scribd.com/doc/228155369/The-Segmented-Sieve-of-Zakiya-SSoZ
271
- [3] https://www.scribd.com/doc/73385696/The-Sieve-of-Zakiya
272
- [4] https://en.wikipedia.org/wiki/GNU_Core_Utilities
273
- [5] https://en.wikipedia.org/wiki/Factor_(Unix)
274
- [6] https://en.wikipedia.org/wiki/Miller-Rabin_primality_test
259
+ [1] https://www.scribd.com/doc/150217723/Improved-Primality-Testing-and-Factorization-in-Ruby-revised
260
+
261
+ [2] https://www.scribd.com/doc/228155369/The-Segmented-Sieve-of-Zakiya-SSoZ
262
+
263
+ [3] https://www.scribd.com/doc/73385696/The-Sieve-of-Zakiya
264
+
265
+ [4] https://en.wikipedia.org/wiki/GNU_Core_Utilities
266
+
267
+ [5] https://en.wikipedia.org/wiki/Factor_(Unix)
268
+
269
+ [6] https://en.wikipedia.org/wiki/Miller-Rabin_primality_test
270
+
275
271
  [7] https://www.academia.edu/105821370/Twin_Primes_Segmented_Sieve_of_Zakiya_SSoZ_Explained_Review_Article
276
272
 
277
273
  ## License
278
- LGPL-2.0-or-later
274
+ GPLv2+
@@ -1,5 +1,5 @@
1
1
  module Primes
2
2
  module Utils
3
- VERSION = "3.1.1"
3
+ VERSION = "3.1.3"
4
4
  end
5
5
  end
data/lib/primes/utils.rb CHANGED
@@ -1,4 +1,7 @@
1
- # Enable YJIT if using CRuby >= 3.3"
1
+ # Updated: 2026nov19
2
+ # Changed: for nex|prevprime return 0, not nil, for invalid cases
3
+ # See: https://jpcamara.com/2024/12/01/speeding-up-ruby.html
4
+ # Enable YJIT if using CRuby >= 3.3"
2
5
  RubyVM::YJIT.enable if RUBY_ENGINE == "ruby" and RUBY_VERSION.to_f >= 3.3
3
6
 
4
7
  require "bitarray"
@@ -25,7 +28,7 @@ module Primes
25
28
  def factors
26
29
  factors = self < 0 ? [-1] : []
27
30
  factors += `factor #{abs}`.split(' ')[1..-1].map(&:to_i)
28
- factors.group_by { |prm| prm }.map { |prm, exp| [prm, exp.size] }
31
+ factors.tally.to_a
29
32
  end
30
33
 
31
34
  alias prime_division factors
@@ -52,8 +55,8 @@ module Primes
52
55
  (factors << prime; num /= prime; break) if (num % prime).zero?
53
56
  (r = r0; modk += modpg) if (r = r.succ) == rescnt
54
57
  end end end
55
- factors << num if num > 1
56
- factors.group_by{ |prm| prm}.map{ |prm, exp| [prm, exp.size] }
58
+ factors << num if num > 1 # include last prime factored value if > 1
59
+ factors.tally.to_a
57
60
  end
58
61
 
59
62
  # Use pure Ruby version of `factor` if not in OS.
@@ -62,7 +65,7 @@ module Primes
62
65
  # Return value of nth prime for self >= 1, or nil if self < 1
63
66
  # Adaptively selects best SP PG, unless valid input PG given at runtime
64
67
  def primenth(p = 0)
65
- return nil if (n = self) < 1
68
+ return 0 if (n = self) < 1
66
69
  seeds = PRIMES
67
70
  return seeds[n - 1] if n <= seeds.size
68
71
 
@@ -167,7 +170,7 @@ module Primes
167
170
  end
168
171
 
169
172
  # PGT and Miller-Rabin combined primality tests for random n
170
- def prime? (k = 5) # Can change k up|down for primemr?
173
+ def prime?(k = 5) # Can change k up|down for primemr?
171
174
  # Use PGT residue checks for small values < PRIMES.last**2
172
175
  return PRIMES.include? self if self <= PRIMES.last
173
176
  return false if MODPN.gcd(self) != 1
@@ -177,7 +180,7 @@ module Primes
177
180
 
178
181
  # Returns the next prime number for self >= 0, or nil if n < 0
179
182
  def next_prime
180
- return nil if (n = self) < 0 # return nil if n negative
183
+ return 0 if (n = self) < 0 # return 0 if n negative
181
184
  return (n >> 1) + 2 if n <= 2 # return 2 or 3 if n is 0|1|2
182
185
  n = n + 1 | 1 # 1st odd number > n
183
186
  until (res = n % 6) & 0b11 == 1; n += 2 end # n first P3 pc >= n, w/residue 1 or 5
@@ -188,7 +191,7 @@ module Primes
188
191
 
189
192
  # Returns the previous prime number < self, or nil if self <= 2
190
193
  def prev_prime
191
- return nil if (n = self) <= 2 # no primes for n <= 2
194
+ return 0 if (n = self) <= 2 # no primes for n <= 2
192
195
  return (n >> 1) + 1 if n <= 5 # 5|4 -> 3, 3 -> 2
193
196
  n = n - 2 | 1 # 1st odd number < n
194
197
  until (res = n % 6) & 0b11 == 1; n -= 2 end # n first P3 pc <= n, w/residue 1 or 5
@@ -271,20 +274,24 @@ module Primes
271
274
  residues.sort << (modpg - 1) << (modpg + 1)
272
275
  end
273
276
 
274
- # Determine number of pcs upto the effective start|end vals; w/flag
277
+ # Determine number of pcs upto the effective start, end, and range width.
275
278
  # The effective start_num is first pc >= start_num, first pc <= end_num,
276
279
  # and effective range is number of pcs between them (inclusive).
277
- # inputs: end_num|start_num of range, PGs residues array, start:t|end:f flag
278
- # outputs: pcs_to_num - number of pcs < start_num or <= end_num
279
- # r - residue index for effective start_num pc
280
- # modk - mod resgroup value for effective num value
281
- def pcs_to_num(num, residues, flag)
282
- return [0, 0, 0] if num < residues[0]
280
+ # inputs: end_num and start_num of range, and PGs residues array
281
+ # outputs: pcs_to_end - number of pcs in <= end_num pc for PG
282
+ # pcs_to_start - number of pcs < effective start_num pc for range
283
+ # r1 - residue index for effective start_num pc
284
+ # modk1 - mod resgroup value for effective start_num pc
285
+ # pcs_in_range - total number of pcs in effective range
286
+ def pcs_to_nums(end_num, start_num, residues)
283
287
  modpg, rescnt = residues[-1] - 1, residues.size
284
- val1, val2 = flag ? [num-2, num-1] : [(num-1)|1, (num-1)|1]
285
- k = val1 / modpg; modk = k * modpg; r = 0; resk = val2 - modk
286
- r = r.succ while resk >= residues[r]; pcs_to_num = k * rescnt + r
287
- [pcs_to_num, r, modk]
288
+ end_num, k2 = end_num < residues[0] ? [1, 0] : [(end_num - 1)|1, ((end_num - 1)|1)/modpg]
289
+ start_num, k1 = start_num < residues[0] ? [1, 0] : [start_num - 1, (start_num - 2)/modpg]
290
+ r1, r2, resk1, resk2 = 0, 0, start_num - (modk1 = k1 * modpg), end_num - (k2 * modpg)
291
+ r1 = r1.succ while resk1 >= residues[r1]
292
+ r2 = r2.succ while resk2 >= residues[r2]
293
+ pcs_to_end, pcs_to_start = k2 * rescnt + r2, k1 * rescnt + r1
294
+ [pcs_to_end, pcs_to_start, r1, modk1, (pcs_in_range = pcs_to_end - pcs_to_start)]
288
295
  end
289
296
 
290
297
  # Select SP Prime Generator to parametize the pcs within inputs range
@@ -300,9 +307,11 @@ module Primes
300
307
  elsif range < 24_494_897; 510_510 # P17; Math.isqrt(600_000_000_000_000)
301
308
  else 9_699_690 # P19
302
309
  end
303
- residues = make_residues(modpg) # chosen PG residues
310
+ residues = make_residues(modpg) # chosen PG residues
304
311
  primes = PRIMES.select { |p| p < residues[0] && p.between?(start_num, end_num) }
305
- _, r, modk = pcs_to_num(start_num, residues, true)
312
+ start_num = start_num < residues[0] ? 1 : start_num - 1
313
+ k = (start_num - 1) / modpg; modk = k * modpg; r = 0; resk = start_num - modk
314
+ r = r.succ while resk >= residues[r]
306
315
  [r, modk, residues, primes]
307
316
  end
308
317
 
@@ -315,12 +324,10 @@ module Primes
315
324
  # pcs2start- number of pcs < start_num pc
316
325
  # rs - residue index location for first pc >= start_num
317
326
  def sozcore2(end_num, start_num, modpg)
318
- residues = make_residues(modpg)
319
- rescnt, sqrtN = residues.size, Integer.sqrt(end_num)
320
- pcs_to_start, rs, modks = pcs_to_num(start_num, residues, true) # num pcs < start_num
321
- maxpcs, _ = pcs_to_num(end_num, residues, false) # num pcs <= end_num
322
- pcs_to_sqrtN, _ = pcs_to_num(sqrtN, residues, false) # num pcs <= sqrtN
323
- inputs_range, pcs_range = end_num - start_num, maxpcs - pcs_to_start
327
+ residues = make_residues(modpg); rescnt = residues.size
328
+ pcs_to_end, pcs_to_start, rs, modks, pcs_range = pcs_to_nums(end_num, start_num, residues)
329
+ sqrtN, maxpcs, inputs_range = Integer.sqrt(end_num), pcs_to_end, end_num - start_num
330
+ pcs_to_sqrtN, _ = pcs_to_nums(sqrtN, 0, residues) # num pcs <= sqrtN
324
331
 
325
332
  m = pcs_to_start # index to start retrieving primes in prms array
326
333
  split_arrays = (start_num > sqrtN) # flag, true for split arrays
@@ -2,7 +2,7 @@
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'primes/utils/version'
5
- require 'rake'
5
+ require "rake"
6
6
 
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = "primes-utils"
@@ -14,12 +14,17 @@ Gem::Specification.new do |spec|
14
14
  spec.description = %q{Methods: prime?, primemr?, primes, primesmr, primescnt, primescntmr, primenth|nthprime, factors|prime_division, factors1, next_prime, prev_prime, primes_utils}
15
15
  spec.homepage = "https://github.com/jzakiya/primes-utils"
16
16
 
17
- spec.files = FileList['lib/primes/utils.rb', 'lib/primes/utils/*.rb','bin/*', 'README.md', 'Gemfile', 'Rakefile', 'CODE_OF_CONDUCT.md', 'primes-utils-3.0.0.gemspec']
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ #spec.bindir = "exe"
19
+ #spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
20
  spec.require_paths = ["lib"]
19
21
  spec.license = "LGPL-2.0-or-later"
20
22
  spec.required_ruby_version = ">= 3.0.0"
21
23
 
22
- spec.add_dependency "bitarray", "~> 1.3", ">= 1.3.1"
24
+ #if spec.respond_to?(:metadata)
25
+ # 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."
26
+ #end
27
+
23
28
  spec.add_development_dependency "bundler", "~> 1.9"
24
29
  spec.add_development_dependency "rake", "~> 10.0"
25
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: primes-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.1
4
+ version: 3.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jabari Zakiya
@@ -9,26 +9,6 @@ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: bitarray
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - "~>"
17
- - !ruby/object:Gem::Version
18
- version: '1.3'
19
- - - ">="
20
- - !ruby/object:Gem::Version
21
- version: 1.3.1
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- requirements:
26
- - - "~>"
27
- - !ruby/object:Gem::Version
28
- version: '1.3'
29
- - - ">="
30
- - !ruby/object:Gem::Version
31
- version: 1.3.1
32
12
  - !ruby/object:Gem::Dependency
33
13
  name: bundler
34
14
  requirement: !ruby/object:Gem::Requirement
@@ -65,6 +45,7 @@ executables: []
65
45
  extensions: []
66
46
  extra_rdoc_files: []
67
47
  files:
48
+ - ".gitignore"
68
49
  - CODE_OF_CONDUCT.md
69
50
  - Gemfile
70
51
  - README.md
@@ -73,7 +54,7 @@ files:
73
54
  - bin/setup
74
55
  - lib/primes/utils.rb
75
56
  - lib/primes/utils/version.rb
76
- - primes-utils-3.0.0.gemspec
57
+ - primes-utils.gemspec
77
58
  homepage: https://github.com/jzakiya/primes-utils
78
59
  licenses:
79
60
  - LGPL-2.0-or-later
@@ -92,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
73
  - !ruby/object:Gem::Version
93
74
  version: '0'
94
75
  requirements: []
95
- rubygems_version: 4.0.3
76
+ rubygems_version: 4.0.20
96
77
  specification_version: 4
97
78
  summary: suite of extremely fast utility methods for testing and generating primes
98
79
  test_files: []