-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpigeon.hh
83 lines (72 loc) · 1.88 KB
/
pigeon.hh
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
#ifndef _PIGEON_HH_
#define _PIGEON_HH_ 1
#include <gmpxx.h>
class pigeon {
class iterator;
public:
pigeon(std::size_t = -1) noexcept;
iterator begin() const noexcept;
iterator end() const noexcept;
private:
class iterator {
public:
iterator() noexcept;
iterator(std::size_t) noexcept;
long operator*() const noexcept;
bool operator!=(const iterator &) const noexcept;
iterator &operator++() noexcept;
iterator operator++(int) noexcept;
private:
mpz_class q, r, t, k, n, l;
long p;
std::size_t digits;
};
std::size_t digits;
};
pigeon::pigeon(std::size_t digits) noexcept
: digits{digits} {
}
pigeon::iterator pigeon::begin() const noexcept {
return pigeon::iterator();
}
pigeon::iterator pigeon::end() const noexcept {
return pigeon::iterator(this->digits);
}
pigeon::iterator::iterator() noexcept
: q{10}, r{-30}, t{3}, k{2}, n{0}, l{5}, p{3}, digits{0} {
}
pigeon::iterator::iterator(std::size_t digits) noexcept
: digits{digits} {
}
long pigeon::iterator::operator*() const noexcept {
return this->p;
}
bool pigeon::iterator::operator!=(const iterator &that) const noexcept {
return this->digits != that.digits;
}
pigeon::iterator &pigeon::iterator::operator++() noexcept {
while (true) {
if (this->q << 2 < this->n * this->t + this->t - this->r) {
this->p = n.get_si();
this->q *= 10;
this->r -= this->n * this->t;
this->r *= 10;
this->n = (3 * this->q + this->r) / this->t;
++this->digits;
break;
} else {
this->t *= this->l;
mpz_class qk = this->q * this->k;
mpz_class q2 = 2 * this->q;
mpz_class rx = this->r * this->l;
this->n = (7 * qk + q2 + rx) / this->t;
this->r = std::move(rx);
this->r += q2 * this->l;
this->q = std::move(qk);
++this->k;
this->l += 2;
}
}
return *this;
}
#endif // _PIGEON_HH_