Skip to content

Commit 8d3d3a1

Browse files
committed
cargo fmt
1 parent e70edf3 commit 8d3d3a1

File tree

1 file changed

+105
-64
lines changed

1 file changed

+105
-64
lines changed

src/lib.rs

+105-64
Original file line numberDiff line numberDiff line change
@@ -62,49 +62,48 @@ case, but slower in the long term as they do not use any caching of primes.
6262
*/
6363
#![doc(html_root_url = "https://wackywendell.github.io/primes/")]
6464

65+
use std::cmp::Ordering::{Equal, Greater, Less};
6566
#[warn(non_camel_case_types)]
6667
#[warn(non_snake_case)]
6768
#[warn(unused_qualifications)]
6869
#[warn(non_upper_case_globals)]
6970
#[warn(missing_docs)]
70-
7171
use std::ops::Index;
7272
use std::slice;
73-
use std::cmp::Ordering::{Equal,Less,Greater};
7473

7574
/** A prime generator, using the Sieve of Eratosthenes.
7675
7776
Create with `let mut pset = PrimeSet::new()`, and then use `pset.iter()` to iterate over all primes.
7877
**/
7978
pub struct PrimeSet {
80-
lst : Vec<u64>
79+
lst: Vec<u64>,
8180
}
8281

8382
/// An iterator over generated primes. Created by PrimeSet::iter or
8483
/// PrimeSet::generator
8584
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,
8988
}
9089

9190
impl PrimeSet {
9291
/// A new prime generator, primed with 2 and 3
9392
pub fn new() -> PrimeSet {
94-
PrimeSet{lst:vec!(2,3)}
93+
PrimeSet { lst: vec![2, 3] }
9594
}
9695

9796
/// Finds one more prime, and adds it to the list
9897
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;
10099
let mut remainder = 0;
101100
loop {
102101
for &n in self.lst.iter() {
103102
remainder = l % n;
104-
if remainder == 0 || n*n > l {
103+
if remainder == 0 || n * n > l {
105104
break;
106105
}
107-
};
106+
}
108107

109108
if remainder != 0 {
110109
self.lst.push(l);
@@ -128,17 +127,25 @@ impl PrimeSet {
128127
/// Iterator over all primes not yet found
129128
pub fn generator<'a>(&'a mut self) -> PrimeSetIter<'a> {
130129
let myn = self.len();
131-
PrimeSetIter{p:self, n:myn, expand:true}
130+
PrimeSetIter {
131+
p: self,
132+
n: myn,
133+
expand: true,
134+
}
132135
}
133136

134137
/// Iterator over all primes, starting with 2. If you don't care about the "state" of the
135138
/// PrimeSet, this is what you want!
136139
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+
}
138145
}
139146

140147
//~ 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}
142149
//~ }
143150

144151
/// Iterator over just the primes found so far
@@ -150,7 +157,7 @@ impl PrimeSet {
150157
/// Returns (idx, prime)
151158
/// Note that if n is prime, then the output will be (idx, n)
152159
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)) {
154161
self.expand();
155162
}
156163
self.find_vec(n).unwrap()
@@ -160,11 +167,19 @@ impl PrimeSet {
160167
/// Note that this only requires primes up to n.sqrt() to be generated, and will generate
161168
/// them as necessary on its own.
162169
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!
165176
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+
};
168183
}
169184
unreachable!("This iterator should not be empty.");
170185
}
@@ -173,10 +188,12 @@ impl PrimeSet {
173188
/// Returns (idx, prime)
174189
/// Note that if n is prime, then the output will be (idx, n)
175190
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+
}
177194

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();
180197

181198
// Binary search algorithm
182199
while lim != 0 {
@@ -187,42 +204,46 @@ impl PrimeSet {
187204
base = ix + 1;
188205
lim -= 1;
189206
}
190-
Greater => ()
207+
Greater => (),
191208
}
192209
lim >>= 1;
193210
}
194211
return Some((base, self.lst[base]));
195212
}
196213

197214
/// 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+
}
202219
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 {
212231
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+
}
217238

218-
if p*p > curn {
239+
if p * p > curn {
219240
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+
}
226247
}
227248

228249
impl Index<usize> for PrimeSet {
@@ -235,62 +256,82 @@ impl Index<usize> for PrimeSet {
235256
impl<'a> Iterator for PrimeSetIter<'a> {
236257
type Item = u64;
237258
fn next(&mut self) -> Option<u64> {
238-
while self.n >= self.p.len(){
259+
while self.n >= self.p.len() {
239260
match self.expand {
240261
true => self.p.expand(),
241-
false => return None
262+
false => return None,
242263
}
243264
}
244265
self.n += 1;
245266

246-
let m = self.p.lst[self.n-1];
267+
let m = self.p.lst[self.n - 1];
247268

248269
Some(m)
249270
}
250271
}
251272

252273
/// Find the first factor (other than 1) of a number
253274
fn firstfac(x: u64) -> u64 {
254-
if x % 2 == 0 { return 2; };
275+
if x % 2 == 0 {
276+
return 2;
277+
};
255278
// TODO: return to step_by
256279
// 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+
};
259284
}
260285
return x;
261286
}
262287

263288
/// Find all prime factors of a number
264289
/// Does not use a PrimeSet, but simply counts upwards
265290
pub fn factors(x: u64) -> Vec<u64> {
266-
if x <= 1 {return vec!()};
291+
if x <= 1 {
292+
return vec![];
293+
};
267294
let mut lst: Vec<u64> = Vec::new();
268295
let mut curn = x;
269-
loop {
296+
loop {
270297
let m = firstfac(curn);
271298
lst.push(m);
272-
if m == curn { break } else { curn /= m };
299+
if m == curn {
300+
break;
301+
} else {
302+
curn /= m
303+
};
273304
}
274-
return lst
305+
return lst;
275306
}
276307

277308
/// Find all unique prime factors of a number
278309
pub fn factors_uniq(x: u64) -> Vec<u64> {
279-
if x <= 1 {return vec!()};
310+
if x <= 1 {
311+
return vec![];
312+
};
280313
let mut lst: Vec<u64> = Vec::new();
281314
let mut curn = x;
282-
loop {
315+
loop {
283316
let m = firstfac(curn);
284317
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+
}
288327
}
289-
return lst
328+
return lst;
290329
}
291330

292331
/// 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+
}
295336
firstfac(n) == n
296337
}

0 commit comments

Comments
 (0)