@@ -62,49 +62,48 @@ case, but slower in the long term as they do not use any caching of primes.
62
62
*/
63
63
#![ doc( html_root_url = "https://wackywendell.github.io/primes/" ) ]
64
64
65
+ use std:: cmp:: Ordering :: { Equal , Greater , Less } ;
65
66
#[ warn( non_camel_case_types) ]
66
67
#[ warn( non_snake_case) ]
67
68
#[ warn( unused_qualifications) ]
68
69
#[ warn( non_upper_case_globals) ]
69
70
#[ warn( missing_docs) ]
70
-
71
71
use std:: ops:: Index ;
72
72
use std:: slice;
73
- use std:: cmp:: Ordering :: { Equal , Less , Greater } ;
74
73
75
74
/** A prime generator, using the Sieve of Eratosthenes.
76
75
77
76
Create with `let mut pset = PrimeSet::new()`, and then use `pset.iter()` to iterate over all primes.
78
77
**/
79
78
pub struct PrimeSet {
80
- lst : Vec < u64 >
79
+ lst : Vec < u64 > ,
81
80
}
82
81
83
82
/// An iterator over generated primes. Created by PrimeSet::iter or
84
83
/// PrimeSet::generator
85
84
pub struct PrimeSetIter < ' a > {
86
- p : & ' a mut PrimeSet ,
87
- n : usize ,
88
- expand : bool
85
+ p : & ' a mut PrimeSet ,
86
+ n : usize ,
87
+ expand : bool ,
89
88
}
90
89
91
90
impl PrimeSet {
92
91
/// A new prime generator, primed with 2 and 3
93
92
pub fn new ( ) -> PrimeSet {
94
- PrimeSet { lst : vec ! ( 2 , 3 ) }
93
+ PrimeSet { lst : vec ! [ 2 , 3 ] }
95
94
}
96
95
97
96
/// Finds one more prime, and adds it to the list
98
97
pub fn expand ( & mut self ) {
99
- let mut l : u64 = self . lst [ self . lst . len ( ) - 1 ] + 2 ;
98
+ let mut l: u64 = self . lst [ self . lst . len ( ) - 1 ] + 2 ;
100
99
let mut remainder = 0 ;
101
100
loop {
102
101
for & n in self . lst . iter ( ) {
103
102
remainder = l % n;
104
- if remainder == 0 || n* n > l {
103
+ if remainder == 0 || n * n > l {
105
104
break ;
106
105
}
107
- } ;
106
+ }
108
107
109
108
if remainder != 0 {
110
109
self . lst . push ( l) ;
@@ -128,17 +127,25 @@ impl PrimeSet {
128
127
/// Iterator over all primes not yet found
129
128
pub fn generator < ' a > ( & ' a mut self ) -> PrimeSetIter < ' a > {
130
129
let myn = self . len ( ) ;
131
- PrimeSetIter { p : self , n : myn, expand : true }
130
+ PrimeSetIter {
131
+ p : self ,
132
+ n : myn,
133
+ expand : true ,
134
+ }
132
135
}
133
136
134
137
/// Iterator over all primes, starting with 2. If you don't care about the "state" of the
135
138
/// PrimeSet, this is what you want!
136
139
pub fn iter < ' a > ( & ' a mut self ) -> PrimeSetIter < ' a > {
137
- PrimeSetIter { p : self , n : 0 , expand : true }
140
+ PrimeSetIter {
141
+ p : self ,
142
+ n : 0 ,
143
+ expand : true ,
144
+ }
138
145
}
139
146
140
147
//~ pub fn iter_once(&'self mut self) -> PrimeSetIter<'self> {
141
- //~ PrimeSetIter{p:self, n:0, expand:false}
148
+ //~ PrimeSetIter{p:self, n:0, expand:false}
142
149
//~ }
143
150
144
151
/// Iterator over just the primes found so far
@@ -150,7 +157,7 @@ impl PrimeSet {
150
157
/// Returns (idx, prime)
151
158
/// Note that if n is prime, then the output will be (idx, n)
152
159
pub fn find ( & mut self , n : u64 ) -> ( usize , u64 ) {
153
- while n > * ( self . lst . last ( ) . unwrap_or ( & 0 ) ) {
160
+ while n > * ( self . lst . last ( ) . unwrap_or ( & 0 ) ) {
154
161
self . expand ( ) ;
155
162
}
156
163
self . find_vec ( n) . unwrap ( )
@@ -160,11 +167,19 @@ impl PrimeSet {
160
167
/// Note that this only requires primes up to n.sqrt() to be generated, and will generate
161
168
/// them as necessary on its own.
162
169
pub fn is_prime ( & mut self , n : u64 ) -> bool {
163
- if n <= 1 { return false ; }
164
- if n == 2 { return true ; } // otherwise we get 2 % 2 == 0!
170
+ if n <= 1 {
171
+ return false ;
172
+ }
173
+ if n == 2 {
174
+ return true ;
175
+ } // otherwise we get 2 % 2 == 0!
165
176
for m in self . iter ( ) {
166
- if n % m == 0 { return false ; } ;
167
- if m* m > n { return true ; } ;
177
+ if n % m == 0 {
178
+ return false ;
179
+ } ;
180
+ if m * m > n {
181
+ return true ;
182
+ } ;
168
183
}
169
184
unreachable ! ( "This iterator should not be empty." ) ;
170
185
}
@@ -173,10 +188,12 @@ impl PrimeSet {
173
188
/// Returns (idx, prime)
174
189
/// Note that if n is prime, then the output will be (idx, n)
175
190
pub fn find_vec ( & self , n : u64 ) -> Option < ( usize , u64 ) > {
176
- if n > * ( self . lst . last ( ) . unwrap_or ( & 0 ) ) { return None ; }
191
+ if n > * ( self . lst . last ( ) . unwrap_or ( & 0 ) ) {
192
+ return None ;
193
+ }
177
194
178
- let mut base : usize = 0 ;
179
- let mut lim : usize = self . len ( ) ;
195
+ let mut base: usize = 0 ;
196
+ let mut lim: usize = self . len ( ) ;
180
197
181
198
// Binary search algorithm
182
199
while lim != 0 {
@@ -187,42 +204,46 @@ impl PrimeSet {
187
204
base = ix + 1 ;
188
205
lim -= 1 ;
189
206
}
190
- Greater => ( )
207
+ Greater => ( ) ,
191
208
}
192
209
lim >>= 1 ;
193
210
}
194
211
return Some ( ( base, self . lst [ base] ) ) ;
195
212
}
196
213
197
214
/// Get the nth prime, even if we haven't yet found it
198
- pub fn get ( & mut self , index : usize ) -> u64 {
199
- for _ in ( 0 ..( index as isize ) + 1 - ( self . lst . len ( ) as isize ) ) {
200
- self . expand ( ) ;
201
- }
215
+ pub fn get ( & mut self , index : usize ) -> u64 {
216
+ for _ in 0 ..( index as isize ) + 1 - ( self . lst . len ( ) as isize ) {
217
+ self . expand ( ) ;
218
+ }
202
219
self . lst [ index]
203
- }
204
-
205
- /// Get the prime factors of a number, starting from 2, including repeats
206
- pub fn prime_factors ( & mut self , n : u64 ) -> Vec < u64 > {
207
- if n == 1 { return Vec :: new ( ) ; }
208
- let mut curn = n;
209
- let mut lst: Vec < u64 > = Vec :: new ( ) ;
210
- for p in self . iter ( ) {
211
- while curn % p == 0 {
220
+ }
221
+
222
+ /// Get the prime factors of a number, starting from 2, including repeats
223
+ pub fn prime_factors ( & mut self , n : u64 ) -> Vec < u64 > {
224
+ if n == 1 {
225
+ return Vec :: new ( ) ;
226
+ }
227
+ let mut curn = n;
228
+ let mut lst: Vec < u64 > = Vec :: new ( ) ;
229
+ for p in self . iter ( ) {
230
+ while curn % p == 0 {
212
231
println ! ( "Pushing {} ({} / {})" , p, curn, n) ;
213
- lst. push ( p) ;
214
- curn /= p;
215
- if curn == 1 { return lst; }
216
- }
232
+ lst. push ( p) ;
233
+ curn /= p;
234
+ if curn == 1 {
235
+ return lst;
236
+ }
237
+ }
217
238
218
- if p* p > curn {
239
+ if p * p > curn {
219
240
println ! ( "Final push {} ({} / {})" , p, curn, n) ;
220
- lst. push ( curn) ;
221
- return lst;
222
- }
223
- }
224
- unreachable ! ( "This should be unreachable." ) ;
225
- }
241
+ lst. push ( curn) ;
242
+ return lst;
243
+ }
244
+ }
245
+ unreachable ! ( "This should be unreachable." ) ;
246
+ }
226
247
}
227
248
228
249
impl Index < usize > for PrimeSet {
@@ -235,62 +256,82 @@ impl Index<usize> for PrimeSet {
235
256
impl < ' a > Iterator for PrimeSetIter < ' a > {
236
257
type Item = u64 ;
237
258
fn next ( & mut self ) -> Option < u64 > {
238
- while self . n >= self . p . len ( ) {
259
+ while self . n >= self . p . len ( ) {
239
260
match self . expand {
240
261
true => self . p . expand ( ) ,
241
- false => return None
262
+ false => return None ,
242
263
}
243
264
}
244
265
self . n += 1 ;
245
266
246
- let m = self . p . lst [ self . n - 1 ] ;
267
+ let m = self . p . lst [ self . n - 1 ] ;
247
268
248
269
Some ( m)
249
270
}
250
271
}
251
272
252
273
/// Find the first factor (other than 1) of a number
253
274
fn firstfac ( x : u64 ) -> u64 {
254
- if x % 2 == 0 { return 2 ; } ;
275
+ if x % 2 == 0 {
276
+ return 2 ;
277
+ } ;
255
278
// TODO: return to step_by
256
279
// for n in (3..).step_by(2).take_while(|m| m*m <= x) {
257
- for n in ( 1 ..) . map ( |m| 2 * m+1 ) . take_while ( |m| m* m <= x) {
258
- if x % n == 0 { return n; } ;
280
+ for n in ( 1 ..) . map ( |m| 2 * m + 1 ) . take_while ( |m| m * m <= x) {
281
+ if x % n == 0 {
282
+ return n;
283
+ } ;
259
284
}
260
285
return x;
261
286
}
262
287
263
288
/// Find all prime factors of a number
264
289
/// Does not use a PrimeSet, but simply counts upwards
265
290
pub fn factors ( x : u64 ) -> Vec < u64 > {
266
- if x <= 1 { return vec ! ( ) } ;
291
+ if x <= 1 {
292
+ return vec ! [ ] ;
293
+ } ;
267
294
let mut lst: Vec < u64 > = Vec :: new ( ) ;
268
295
let mut curn = x;
269
- loop {
296
+ loop {
270
297
let m = firstfac ( curn) ;
271
298
lst. push ( m) ;
272
- if m == curn { break } else { curn /= m } ;
299
+ if m == curn {
300
+ break ;
301
+ } else {
302
+ curn /= m
303
+ } ;
273
304
}
274
- return lst
305
+ return lst;
275
306
}
276
307
277
308
/// Find all unique prime factors of a number
278
309
pub fn factors_uniq ( x : u64 ) -> Vec < u64 > {
279
- if x <= 1 { return vec ! ( ) } ;
310
+ if x <= 1 {
311
+ return vec ! [ ] ;
312
+ } ;
280
313
let mut lst: Vec < u64 > = Vec :: new ( ) ;
281
314
let mut curn = x;
282
- loop {
315
+ loop {
283
316
let m = firstfac ( curn) ;
284
317
lst. push ( m) ;
285
- if curn == m { break ; }
286
- while curn % m == 0 { curn /= m; }
287
- if curn == 1 { break ; }
318
+ if curn == m {
319
+ break ;
320
+ }
321
+ while curn % m == 0 {
322
+ curn /= m;
323
+ }
324
+ if curn == 1 {
325
+ break ;
326
+ }
288
327
}
289
- return lst
328
+ return lst;
290
329
}
291
330
292
331
/// Test whether a number is prime. Checks every odd number up to sqrt(n).
293
- pub fn is_prime ( n : u64 ) -> bool {
294
- if n <= 1 { return false ; }
332
+ pub fn is_prime ( n : u64 ) -> bool {
333
+ if n <= 1 {
334
+ return false ;
335
+ }
295
336
firstfac ( n) == n
296
337
}
0 commit comments