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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use std::ops::{Add, AddAssign, Mul, Neg};

pub struct FracApproxIter<I, F> {
    lower: Fraction<I>,
    upper: Fraction<I>,
    bound: Option<I>,
    pred: F,
}

impl<I: SbInt, F: Fn(I, I) -> bool> FracApproxIter<I, F> {
    fn new(pred: F, bound: Option<I>) -> Self {
        let (lower, upper) = if !I::SIGNED {
            (I::lowest_frac(), I::highest_frac())
        } else if pred(I::ZERO, I::ONE) {
            (I::zero_frac(), I::highest_frac())
        } else {
            (I::lowest_frac(), I::zero_frac())
        };
        let (lower, upper) = (lower.into(), upper.into());
        Self { lower, upper, bound, pred }
    }

    fn iter_return(&mut self, q: I, tf: bool) -> (ApproxFrac<I>, I) {
        (ApproxFrac::from(((self.lower, self.upper), tf)), q)
    }
}

impl<I: SbInt, F: Fn(I, I) -> bool> Iterator for FracApproxIter<I, F> {
    type Item = (ApproxFrac<I>, I);
    fn next(&mut self) -> Option<Self::Item> {
        let Self { lower, upper, bound, pred } = self;
        let pred = |fr: Fraction<I>| {
            let (lower, upper) = fr.into();
            pred(lower, upper)
        };
        let median = *lower + *upper;

        if median.is_deeper(*bound) {
            return None;
        }

        let tf = pred(median);
        let (from, to) = if tf { (*lower, *upper) } else { (*upper, *lower) };

        let mut lo = I::ONE;
        let mut hi = lo + I::ONE;
        while pred(from + to * hi) == tf {
            lo += lo;
            hi += hi;
            if (from + to * lo).is_deeper(*bound) {
                let bound =
                    if let Some(bound) = *bound { bound } else { continue };
                let steps = bound.steps(from.into_inner(), to.into_inner());
                let front = from + to * steps;
                *(if tf { lower } else { upper }) = front;
                return Some(self.iter_return(steps, tf));
            }
        }

        while lo.lt1(hi) {
            let mid = lo.avg(hi);
            let cur = pred(from + to * mid) == tf;
            *(if cur { &mut lo } else { &mut hi }) = mid;
        }

        *(if tf { lower } else { upper }) = from + to * lo;
        Some(self.iter_return(lo, tf))
    }
}

pub enum ApproxFrac<I> {
    Lower((I, I)),
    Upper((I, I)),
}
use ApproxFrac::{Lower, Upper};

impl<I: std::fmt::Display> std::fmt::Debug for ApproxFrac<I> {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let (name, (num, den)) = match self {
            Lower(x) => ("Lower", x),
            Upper(x) => ("Upper", x),
        };
        fmt.debug_tuple(name).field(&format!("{}/{}", num, den)).finish()
    }
}

impl<I> ApproxFrac<I> {
    pub fn into_inner(self) -> (I, I) {
        match self {
            Lower(x) | Upper(x) => x,
        }
    }
}

impl<I: SbInt> From<((Fraction<I>, Fraction<I>), bool)> for ApproxFrac<I> {
    fn from(((lower, upper), tf): ((Fraction<I>, Fraction<I>), bool)) -> Self {
        if tf { Lower(lower.into()) } else { Upper(upper.into()) }
    }
}

pub trait FracApprox<I, F> {
    fn frac_approx_iter(self) -> FracApproxIter<I, F>;
    fn frac_approx_iter_bound(self, bound: I) -> FracApproxIter<I, F>;
}

impl<I: SbInt, F: Fn(I, I) -> bool> FracApprox<I, F> for F {
    fn frac_approx_iter(self) -> FracApproxIter<I, F> {
        FracApproxIter::new(self, None)
    }
    fn frac_approx_iter_bound(self, bound: I) -> FracApproxIter<I, F> {
        FracApproxIter::new(self, Some(bound))
    }
}

pub trait SbInt:
    Copy
    + Eq
    + PartialOrd<Self>
    + AddAssign<Self>
    + Add<Self, Output = Self>
    + Mul<Self, Output = Self>
    + std::fmt::Display
    + std::fmt::Debug
{
    const ZERO: Self;
    const ONE: Self;
    const SIGNED: bool;
    fn lt1(self, other: Self) -> bool;
    fn avg(self, other: Self) -> Self;
    fn abs(self) -> Self;
    fn neg(self) -> Self;
    fn steps(self, from: (Self, Self), to: (Self, Self)) -> Self;
    fn lowest_frac() -> (Self, Self);
    fn highest_frac() -> (Self, Self);
    fn zero_frac() -> (Self, Self);
}

#[derive(Clone, Copy, Eq, PartialEq, std::fmt::Debug)]
struct Fraction<I>(I, I);

impl<I: SbInt> From<(I, I)> for Fraction<I> {
    fn from((num, den): (I, I)) -> Self { Self(num, den) }
}

impl<I: SbInt> From<Fraction<I>> for (I, I) {
    fn from(frac: Fraction<I>) -> Self { (frac.0, frac.1) }
}

impl<I: SbInt> Neg for Fraction<I> {
    type Output = Self;
    fn neg(self) -> Self { self.neg() }
}

impl<I: SbInt> Mul<I> for Fraction<I> {
    type Output = Self;
    fn mul(self, a: I) -> Self { Self(self.0 * a, self.1 * a) }
}

impl<I: SbInt> Add<Fraction<I>> for Fraction<I> {
    type Output = Self;
    fn add(self, other: Self) -> Self {
        // mediant
        Self(self.0 + other.0, self.1 + other.1)
    }
}

impl<I: SbInt> Fraction<I> {
    fn is_deeper(self, bound: Option<I>) -> bool {
        bound.into_iter().any(|b| self.1 > b)
    }
    fn neg(self) -> Self { Self(self.0.neg(), self.1) }
    fn into_inner(self) -> (I, I) { (self.0, self.1) }
}

macro_rules! impl_uint {
    ( $($ty:ty)* ) => { $(
        impl SbInt for $ty {
            const ZERO: $ty = 0;
            const ONE: $ty = 1;
            const SIGNED: bool = false;
            fn lt1(self, other: Self) -> bool { self + 1 < other }
            fn avg(self, other: Self) -> Self {
                self + (other - self) / 2
            }
            fn abs(self) -> Self { self }
            fn neg(self) -> Self { self.wrapping_neg() } // not to be called
            fn steps(self, from: (Self, Self), to: (Self, Self)) -> Self {
                if to.0 == 0 {
                    (self - from.1) / to.1
                } else if to.1 == 0 {
                    (self - from.0) / to.0
                } else {
                    ((self - from.0) / to.0).min((self - from.1) / to.1)
                }
            }
            fn lowest_frac() -> (Self, Self) { (0, 1) }
            fn highest_frac() -> (Self, Self) { (1, 0) }
            fn zero_frac() -> (Self, Self) { (0, 1) }
        }
    )* }
}

impl_uint! { u8 u16 u32 u64 u128 usize }

macro_rules! impl_int {
    ( $($ty:ty)* ) => { $(
        impl SbInt for $ty {
            const ZERO: $ty = 0;
            const ONE: $ty = 1;
            const SIGNED: bool = true;
            fn lt1(self, other: Self) -> bool { self + 1 < other }
            fn avg(self, other: Self) -> Self {
                self + (other - self) / 2
            }
            fn abs(self) -> Self { self.abs() }
            fn neg(self) -> Self { -self}
            fn steps(self, from: (Self, Self), to: (Self, Self)) -> Self {
                if to.0 == 0 {
                    (self - from.1) / to.1
                } else if to.1 == 0 {
                    (self - from.0) / to.0
                } else {
                    ((self - from.0) / to.0).min((self - from.1) / to.1)
                }
            }
            fn lowest_frac() -> (Self, Self) { (-1, 0) }
            fn highest_frac() -> (Self, Self) { (1, 0) }
            fn zero_frac() -> (Self, Self) { (0, 1) }
        }
    )* }
}

impl_int! { i8 i16 i32 i64 i128 isize }

#[test]
fn sanity_check() {
    let sqrt2 = |p: i128, q| p * p <= 2 * q * q;
    for ap in sqrt2.frac_approx_iter().take(30) {
        eprintln!("{ap:?}");
    }

    let frac = |p: i128, q| 113 * p <= 355 * q;
    for ap in frac.frac_approx_iter_bound(113) {
        eprintln!("{ap:?}");
    }
}