-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfarey_seq.py
80 lines (58 loc) · 2.03 KB
/
farey_seq.py
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
from typing import Generator
from util import gcd
def farey_sequence(n: int) -> Generator[tuple[int, int], None, None]:
(a, b, c, d) = (0, 1, 1, n)
yield (a, b)
while c <= n:
k = (n + b) // d
(a, b, c, d) = (c, d, k * c - a, k * d - b)
yield (a, b)
def farey_sequence_start_from_between(n: int, a: int, b: int, c: int, d: int) -> Generator[tuple[int, int], None, None]:
yield (a, b)
while c <= n:
k = (n + b) // d
(a, b, c, d) = (c, d, k * c - a, k * d - b)
yield (a, b)
def median_fraction(n1: int, d1: int, n2: int, d2: int) -> tuple[int, int]:
n = n1 + n2
d = d1 + d2
reduce_by = gcd(n, d)
return (n // reduce_by, d // reduce_by)
def are_neighbours(a: int, b: int, c: int, d: int) -> bool:
return b * c - a * d == 1
def less_than(a: int, b: int, c: int, d: int) -> bool:
return a * d - b * c < 0
def less_than_or_equal(a: int, b: int, c: int, d: int) -> bool:
return a * d - b * c <= 0
def search_right_neighbour(a0: int, b0: int, n: int) -> tuple[int, int]:
a, b, c, d = (0, 1, 1, 1)
while True:
numer, denom = median_fraction(a, b, c, d)
if less_than_or_equal(numer, denom, a0, b0):
a, b, = numer, denom
else:
c, d = numer, denom
if (a, b) == (a0, b0) and are_neighbours(a, b, c, d):
break
while True:
numer, denom = median_fraction(a, b, c, d)
if denom > n:
return c, d
else:
c, d = numer, denom
def search_left_neighbour(c0: int, d0: int, n: int) -> tuple[int, int]:
a, b, c, d = (0, 1, 1, 1)
while True:
numer, denom = median_fraction(a, b, c, d)
if less_than(numer, denom, c0, d0):
a, b, = numer, denom
else:
c, d = numer, denom
if (c, d) == (c0, d0) and are_neighbours(a, b, c, d):
break
while True:
numer, denom = median_fraction(a, b, c, d)
if denom > n:
return a, b
else:
a, b = numer, denom