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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::ops::RangeInclusive;
use crate::{K_VALUES, data::utils};
pub struct CoprimeIter {
current: (u64, usize),
stop_after: u64,
}
impl CoprimeIter {
pub fn new(range: RangeInclusive<u64>) -> Self {
let (start, end) = range.into_inner();
let offset = start / 30;
let value = (start % 30) as u8;
let index = utils::unwrap_any(K_VALUES.binary_search(&value));
Self { current: (offset, index), stop_after: end }
}
}
impl Iterator for CoprimeIter {
type Item = u64;
fn next(&mut self) -> Option<Self::Item> {
let (offset, index) = self.current;
let current_value = 30 * offset + (K_VALUES[index] as u64);
if current_value > self.stop_after { return None }
if index < 7 {
self.current = (offset, index + 1);
} else {
self.current = (offset + 1, 0);
}
Some(current_value)
}
}