-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfarey_fraction_approximations.sf
67 lines (57 loc) · 2.41 KB
/
farey_fraction_approximations.sf
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
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 08 February 2018
# https://github.com/trizen
# Rational approximation of a real number, using Farey fractions.
# See also:
# https://en.wikipedia.org/wiki/Farey_sequence
# https://en.wikipedia.org/wiki/Stern%E2%80%93Brocot_tree
var r = sqrt(2) # any real value
var a = 0
var b = 1
var c = 1
var d = 0
for n in (1..30) {
var m = (a+c)/(b+d)
if (m < r) {
(a,b) = m.nude
}
elsif (m > r) {
(c,d) = m.nude
}
else {
break
}
printf("%-49s ≈ %s\n", m, m.as_frac)
}
__END__
1 ≈ 1/1
2 ≈ 2/1
1.5 ≈ 3/2
1.33333333333333333333333333333333333333333333333 ≈ 4/3
1.4 ≈ 7/5
1.42857142857142857142857142857142857142857142857 ≈ 10/7
1.41666666666666666666666666666666666666666666667 ≈ 17/12
1.41176470588235294117647058823529411764705882353 ≈ 24/17
1.41379310344827586206896551724137931034482758621 ≈ 41/29
1.41463414634146341463414634146341463414634146341 ≈ 58/41
1.41428571428571428571428571428571428571428571429 ≈ 99/70
1.41414141414141414141414141414141414141414141414 ≈ 140/99
1.41420118343195266272189349112426035502958579882 ≈ 239/169
1.41422594142259414225941422594142259414225941423 ≈ 338/239
1.4142156862745098039215686274509803921568627451 ≈ 577/408
1.41421143847487001733102253032928942807625649913 ≈ 816/577
1.41421319796954314720812182741116751269035532995 ≈ 1393/985
1.41421392677674084709260588657573582196697774587 ≈ 1970/1393
1.41421362489486963835155592935239697224558452481 ≈ 3363/2378
1.41421349985132322331251858459708593517692536426 ≈ 4756/3363
1.41421355164605469430412820066190559136039017593 ≈ 8119/5741
1.41421357310013548466559921172558196822268752309 ≈ 11482/8119
1.41421356421356421356421356421356421356421356421 ≈ 19601/13860
1.41421356053262588643436559359216366511912657517 ≈ 27720/19601
1.41421356205732046262813424583843877947461223514 ≈ 47321/33461
1.41421356268886963504575135774814564358318716849 ≈ 66922/47321
1.41421356242727340249065385853284147458592260652 ≈ 114243/80782
1.41421356231891669511479915618462400322120392497 ≈ 161564/114243
1.4142135623637995128829637225996667093962312524 ≈ 275807/195025
1.41421356238239058472047482478689808453012432607 ≈ 390050/275807