FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Maths/FareyApproximation.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Maths
/
FareyApproximation.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
50 lines (45 loc) · 1.27 KB
Breadcrumbs
JavaScript
/
Maths
/
FareyApproximation.js
Copy path
File metadata and controls
50 lines (45 loc) · 1.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Reference: https://en.wikipedia.org/wiki/Farey_sequence
* Inspiration: https://www.youtube.com/watch?v=7LKy3lrkTRA
*
* Farey Approximation algorithm is an algorithm to
* approximate a reduced fraction value for a certain
* decimal number x where 0 < x < 1.
*
* The algorithm works by keeping two fractional upper and
* lower bounds which start at 0 / 1 and 1 / 1. These values
* are then used to find the "mediate" which is a value between
* the two fractions.
*
* For any two fractions a / b and c / d,
* mediate = a + c / b + d
*
* Then it is checked if the decimal is greater than or less
* than the mediate and then the lower or the upper value is
* set to be the mediate respectively.
*
* This is repeated for n times and then the mediate is
* returned.
*
* This is explained in a greater detail in the "Inspiration"
* link.
*/
function
fareyApproximation
(
decimal
,
repeat
=
20
)
{
let
a
=
0
let
b
=
1
let
c
=
1
let
d
=
1
let
numerator
let
denominator
for
(
let
i
=
0
;
i
<
repeat
;
i
++
)
{
numerator
=
a
+
c
denominator
=
b
+
d
if
(
decimal
>
numerator
/
denominator
)
{
;
[
a
,
b
]
=
[
numerator
,
denominator
]
}
else
{
;
[
c
,
d
]
=
[
numerator
,
denominator
]
}
}
return
{
numerator
,
denominator
}
}
export
{
fareyApproximation
}
Back
|
FazBrowse Home
|
New Git URL