-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler-07.rs
More file actions
41 lines (34 loc) · 781 Bytes
/
Euler-07.rs
File metadata and controls
41 lines (34 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::mem::replace;
struct Primes {
curr: uint,
primes: Vec<uint>,
}
impl Primes {
fn is_prime(&self, n: uint) -> bool {
!self.primes.iter().any(|&prime| n % prime == 0)
}
fn next_prime(&self) -> uint {
if self.curr == 2 {
3
} else {
let mut n = self.curr + 2;
while !self.is_prime(n) {
n += 2;
}
n
}
}
}
impl Iterator<uint> for Primes {
fn next(&mut self) -> Option<uint> {
let next = self.next_prime();
self.primes.push(next);
Some(replace(&mut self.curr, next))
}
}
fn primes() -> Primes {
Primes { curr: 2, primes: Vec::new() }
}
fn main() {
println!("{}", primes().nth(10_001 - 1).unwrap());
}