primes-utils 3.1.2 → 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 +4 -4
- data/.gitignore +16 -0
- data/README.md +112 -110
- data/lib/primes/utils/version.rb +1 -1
- data/lib/primes/utils.rb +10 -7
- data/{primes-utils-3.0.0.gemspec → primes-utils.gemspec} +8 -3
- metadata +4 -23
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c78862f12ef7e60a4d562eab42073cb8e9773b0cc995da54fb30e7e90a0471cd
|
|
4
|
+
data.tar.gz: 361d35630d1a050a44d181f649cc2dbc86d00de5a419baec98db9087c751e8ff
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ca7e344e0400e4880df99fd6b0455e3763639e706c0e7aba36892328c203f06a638e349ec01884b7c1c51bbf95246ae3024a3af1f609500ef04850a89145a20d
|
|
7
|
+
data.tar.gz: '092ed83b8261aa9fe344fecb8fd0c41ac9755f0cb57f9f69e88bd017df8475ce6a70ddb5e0f3461b5ddedee6b1de858a09204a6b7446824cfc01fd627a3b0978'
|
data/.gitignore
ADDED
data/README.md
CHANGED
|
@@ -2,16 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
## Introduction
|
|
4
4
|
|
|
5
|
-
`primes-utils` is a Rubygem
|
|
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
|
-
|
|
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:
|
|
@@ -34,86 +39,80 @@ Then require as:
|
|
|
34
39
|
|
|
35
40
|
## Methods
|
|
36
41
|
|
|
42
|
+
**prime?**
|
|
43
|
+
|
|
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.
|
|
46
|
+
Uses PGT residues tests, then Miller-Rabin test using `primemr?`.
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
101.prime? => true
|
|
50
|
+
100.prime? => false
|
|
51
|
+
-71.prime? => false
|
|
52
|
+
0.prime? => false
|
|
53
|
+
1.prime? => false
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**primemr?(k=5)**
|
|
57
|
+
|
|
58
|
+
Optimized deterministic (over 64-bits) implementation of Miller-Rabin algorithm.
|
|
59
|
+
Set k > 5 for higher probability for very large numbers.
|
|
37
60
|
|
|
38
61
|
**factors or prime_division**
|
|
39
62
|
|
|
40
|
-
Determine the prime factorization of an
|
|
41
|
-
Uses Unix coreutils function `
|
|
42
|
-
This replaces the `prime_division` method in the `prime.rb` standard library.
|
|
43
|
-
|
|
44
|
-
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]].
|
|
45
67
|
|
|
46
68
|
```
|
|
47
69
|
1111111111111111111.prime_division => [[1111111111111111111, 1]]
|
|
48
|
-
11111111111111111111.prime_division
|
|
70
|
+
11111111111111111111.prime_division => [[11, 1], [41, 1], [101, 1], [271, 1], [3541, 1], [9091, 1], [27961, 1]]
|
|
49
71
|
123456789.factors => [[3, 2], [3607, 1], [3803, 1]]
|
|
50
|
-
|
|
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]]
|
|
51
75
|
0.factors => []
|
|
52
76
|
1.factors => []
|
|
53
77
|
```
|
|
54
78
|
|
|
55
79
|
**factors1**
|
|
56
80
|
|
|
57
|
-
Pure Ruby version equivalent of `
|
|
58
|
-
Not as fast as `
|
|
59
|
-
Always available if OS doesn't have
|
|
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`
|
|
60
84
|
|
|
61
|
-
|
|
62
|
-
123_456_789.factors1 => [[3, 1], [7, 2], [839087, 1]]
|
|
63
|
-
5_000_000_000_000_000_003.factors1 => [[5000000000000000003, 1]]
|
|
64
|
-
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]]
|
|
65
|
-
```
|
|
85
|
+
**primes(start=0), primesmr(start=0)**
|
|
66
86
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
`
|
|
71
|
-
It uses PGT residues tests, then Miller-Rabin test using `primemr?`.
|
|
72
|
-
Default probabilistic reliability constant set at k = 5, set higher if desired for numbers > 128-bts.
|
|
73
|
-
Not really necessary to do though.
|
|
74
|
-
|
|
75
|
-
```
|
|
76
|
-
101.prime? => true
|
|
77
|
-
100.prime? => false
|
|
78
|
-
-71.prime? => false
|
|
79
|
-
0.primemr? => false
|
|
80
|
-
1.primemr? => false
|
|
81
|
-
5_000_000_000_000_000_003.primemr? => true
|
|
82
|
-
987_654_321_012_345_678_901_382_737.prime? 6 => true
|
|
83
|
-
987_654_321_012_345_678_901_382_739.prime? 6 => false
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
**primes(start=0) or 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 SoZ to compute the range primes.
|
|
92
|
-
`primesmr` is slower, but isn't memory limited, especially for very large numbers|ranges.
|
|
93
|
-
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.
|
|
94
91
|
Also see `Error Handling`.
|
|
95
92
|
|
|
96
93
|
```
|
|
97
94
|
50.primes => [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
|
|
98
|
-
|
|
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]
|
|
99
97
|
n=10**100; (n-250).primesmr(n+250) => []
|
|
100
98
|
541.primes.size => 100
|
|
101
99
|
1000.primes(5000).size => 501
|
|
102
100
|
(prms = 1000000.primes(1000100)).size => 6
|
|
101
|
+
prms.size => 6
|
|
103
102
|
prms => [1000003, 1000033, 1000037, 1000039, 1000081, 1000099]
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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 => []
|
|
107
109
|
```
|
|
108
110
|
|
|
109
|
-
**primescnt(start=0)
|
|
111
|
+
**primescnt(start=0), primescntmr(start=0)**
|
|
110
112
|
|
|
111
|
-
Provide count of primes within the
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
`primescnt` is generally faster; uses SoZ to identify|count primes from closest hashed value starting point.
|
|
115
|
-
`primescntmr` is situationally slower, but isn't memory limited, especially for very large numbers|ranges.
|
|
116
|
-
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.
|
|
117
116
|
See `PRIMES-UTILS HANDBOOK` for details on best use practices.
|
|
118
117
|
Also see `Error Handling`.
|
|
119
118
|
|
|
@@ -121,107 +120,103 @@ Also see `Error Handling`.
|
|
|
121
120
|
100001.primescnt => 9592
|
|
122
121
|
100002.primescnt => 9592
|
|
123
122
|
100003.primescnt => 9593
|
|
124
|
-
100000.
|
|
123
|
+
100000.primescntf 100500 => 40
|
|
125
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
|
|
126
128
|
n=10**8; (25*n).primescnt => 121443371
|
|
127
|
-
|
|
128
|
-
0.primescnt => 0
|
|
129
|
+
0.primescntf => 0
|
|
129
130
|
1.primescntmr => 0
|
|
130
|
-
100.primescntmr 100 => 0
|
|
131
|
-
101.primescntmr 101 => 1
|
|
132
131
|
```
|
|
133
132
|
|
|
134
133
|
**primenth(p=0) or nthprime(p=0)**
|
|
135
134
|
|
|
136
|
-
Return value of the nth prime.
|
|
137
|
-
Default Strictly Prime (SP) Prime Generator (PG) is adaptively selected.
|
|
138
|
-
Can change SP PG used on input.
|
|
139
|
-
Indexed nth primes now
|
|
140
|
-
With 16GB mem can compute
|
|
141
|
-
|
|
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`.
|
|
142
142
|
|
|
143
143
|
```
|
|
144
144
|
1000000.primenth => 15485863
|
|
145
145
|
1500000.nthprime => 23879519
|
|
146
|
-
2000000.nthprime
|
|
146
|
+
2000000.nthprime 11 => 32452843
|
|
147
147
|
1122951705.nthprime => 25741879847
|
|
148
|
-
n = 10**11; n.primenth ->
|
|
148
|
+
n = 10**11; n.primenth -> ERROR1: range size too big for available memory. => nil
|
|
149
|
+
0.nthprime => 0
|
|
150
|
+
1.primenth => 2
|
|
149
151
|
2_123_409_000.nthprime => 50092535639
|
|
150
|
-
4_762_719_305.nthprime => 116378528093
|
|
151
|
-
7_123_456_789.nthprime => 177058934933
|
|
152
|
-
1.primenth => 2
|
|
153
|
-
0.nthprime => nil
|
|
154
|
-
-1.nthprime => nil
|
|
155
152
|
```
|
|
153
|
+
|
|
156
154
|
**next_prime**
|
|
157
155
|
|
|
158
|
-
Return value of next prime > n.
|
|
156
|
+
Return value of next prime > n.
|
|
159
157
|
|
|
160
158
|
```
|
|
161
159
|
100.next_prime => 101
|
|
162
160
|
101.next_prime => 103
|
|
163
|
-
0.next_prime => 2
|
|
164
|
-
-1.next_prime => nil
|
|
165
161
|
```
|
|
166
162
|
|
|
167
163
|
**prev_prime**
|
|
168
164
|
|
|
169
|
-
Return value of previous prime < n
|
|
165
|
+
Return value of previous prime < n.
|
|
170
166
|
|
|
171
167
|
```
|
|
172
168
|
102.pref_prime => 101
|
|
173
169
|
101.prev_prime => 97
|
|
174
|
-
|
|
175
|
-
2.prev_prime => nil
|
|
176
|
-
-1.prev_prime => nil
|
|
170
|
+
2.prev_prime => nil
|
|
177
171
|
```
|
|
178
172
|
|
|
179
173
|
**primes_utils**
|
|
180
174
|
|
|
181
|
-
Displays a list of all the `primes-utils` methods available for
|
|
182
|
-
Use as
|
|
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.
|
|
183
177
|
|
|
184
|
-
Available methods for 3.
|
|
178
|
+
Available methods for 3.0.0.
|
|
185
179
|
|
|
186
180
|
```
|
|
187
181
|
0.primes_utils => "prime? primes primesmr primescnt primescntmr primenth|nthprime factors|prime_division factors1 next_prime prev_prime primes_utils"
|
|
188
182
|
```
|
|
189
183
|
|
|
184
|
+
|
|
190
185
|
## Error Handling
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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`.
|
|
195
194
|
|
|
196
195
|
There is also the rare possibility you could get a `NoMemoryError: failed to allocate memory` for the methods
|
|
197
|
-
`
|
|
196
|
+
`primesf` and `primesmr` if their list of numerated primes is bigger than the amount of available system memory needed to store them.
|
|
198
197
|
If those methods are used as designed these errors won't occur, so the extra code isn't justified for them.
|
|
199
198
|
If they occur you will know why now.
|
|
200
199
|
|
|
201
200
|
This behavior is referenced to MRI Ruby.
|
|
202
201
|
|
|
203
202
|
## Coding Implementations
|
|
204
|
-
The
|
|
205
|
-
|
|
206
|
-
|
|
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].
|
|
207
208
|
|
|
208
209
|
Upon loading, the gem tests if the command `factor` exists on the host OS.
|
|
209
|
-
If so, it performs a system call to it within `prime_division|factors`,
|
|
210
|
+
If so, it performs a system call to it within `prime?` and `prime_division|factors`, which uses its output.
|
|
210
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
|
|
211
214
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
All the `primes-utils` methods are `instance_methods` for `Class Integer`.
|
|
215
|
+
All the `primes-utils` methods are `instance_methods` for `class Integer`.
|
|
215
216
|
|
|
216
217
|
## History
|
|
217
218
|
```
|
|
218
|
-
3.
|
|
219
|
-
3.1.1 – some methods refactoring|DRYing, documentation updates
|
|
220
|
-
3.1.0 – major performance enhancements for methods primescnt, primescntmr, and nthprimes,
|
|
221
|
-
primarily by extending nth primes hash values up to 10.1 billion. Can now find nth primes
|
|
222
|
-
over ranges up to 10.1 billion, and thus prime counts up to 253+ billion, in < 30 secs at ~5Ghz.
|
|
223
|
-
3.0.5 – YJIT enabled for Ruby >= 3.3, added new methods: next_prime, prev_prime.
|
|
224
|
-
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,
|
|
225
220
|
2.7.0 – more tweaking adaptive pg selection ranges in select_pg; coded using between? instead of cover?
|
|
226
221
|
2.6.0 – much, much better adaptive pg selection algorithm used in select_pg
|
|
227
222
|
2.5.1 – corrected minor error in select_pg
|
|
@@ -236,7 +231,7 @@ All the `primes-utils` methods are `instance_methods` for `Class Integer`.
|
|
|
236
231
|
significantly faster resulting from sozcore2 changes; massive code cleanups all-arround; added
|
|
237
232
|
private methods select_pg (to adaptively select the pg used in primes), and array_check (used in
|
|
238
233
|
sozcore2 to catch array creation out-of-memory errors)
|
|
239
|
-
2.3.0 – primescnt now finds primes
|
|
234
|
+
2.3.0 – primescnt now finds primes upto some integer much faster, and for much larger integers
|
|
240
235
|
increased index nth primes to over 2 billionth; used in nthprime|primenth and primescnt
|
|
241
236
|
2.2.0 – for sozcore2: refactored to include more common code; changed output api; added memory
|
|
242
237
|
error messages when prms and prms_range arrays creation fails; for primenth: used new
|
|
@@ -256,17 +251,24 @@ All the `primes-utils` methods are `instance_methods` for `Class Integer`.
|
|
|
256
251
|
1.0.1 – check if using Ruby 1.8 at start, if so, require 'rational' library for gcd method
|
|
257
252
|
1.0.0 – initial release April 1, 2015 with methods prime?, primemr?, primes, prime_division|factors, primenth|nthprime
|
|
258
253
|
```
|
|
254
|
+
|
|
259
255
|
## Author
|
|
260
256
|
Jabari Zakiya
|
|
261
257
|
|
|
262
258
|
## References
|
|
263
|
-
[1] https://www.scribd.com/doc/150217723/Improved-Primality-Testing-and-Factorization-in-Ruby-revised
|
|
264
|
-
|
|
265
|
-
[
|
|
266
|
-
|
|
267
|
-
[
|
|
268
|
-
|
|
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
|
+
|
|
269
271
|
[7] https://www.academia.edu/105821370/Twin_Primes_Segmented_Sieve_of_Zakiya_SSoZ_Explained_Review_Article
|
|
270
272
|
|
|
271
273
|
## License
|
|
272
|
-
|
|
274
|
+
GPLv2+
|
data/lib/primes/utils/version.rb
CHANGED
data/lib/primes/utils.rb
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
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.
|
|
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.
|
|
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
|
|
68
|
+
return 0 if (n = self) < 1
|
|
66
69
|
seeds = PRIMES
|
|
67
70
|
return seeds[n - 1] if n <= seeds.size
|
|
68
71
|
|
|
@@ -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
|
|
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
|
|
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
|
|
@@ -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
|
|
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
|
|
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.
|
|
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.
|
|
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
|
|
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.
|
|
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: []
|