From a62b62dfa7ce432ffaab172be79dae6a55cc7f02 Mon Sep 17 00:00:00 2001 From: Rak Laptudirm Date: Mon, 19 Jul 2021 13:04:44 +0530 Subject: [PATCH 1/5] chore: commit `FareyApproximation.js` --- Maths/FareyApproximation.js | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Maths/FareyApproximation.js diff --git a/Maths/FareyApproximation.js b/Maths/FareyApproximation.js new file mode 100644 index 000000000..2c698e2ff --- /dev/null +++ b/Maths/FareyApproximation.js @@ -0,0 +1,45 @@ +/* +* 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 } From cd061ccf25d7d35844993eeb5c47bc7b2e36e8b8 Mon Sep 17 00:00:00 2001 From: Rak Laptudirm Date: Mon, 19 Jul 2021 13:12:47 +0530 Subject: [PATCH 2/5] chore: add tests. --- Maths/test/FareyApproximation.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Maths/test/FareyApproximation.test.js diff --git a/Maths/test/FareyApproximation.test.js b/Maths/test/FareyApproximation.test.js new file mode 100644 index 000000000..bdd07eb64 --- /dev/null +++ b/Maths/test/FareyApproximation.test.js @@ -0,0 +1,13 @@ +import { fareyApproximation } from "../FareyApproximation" + +describe('fareyApproximation', () => { + it('Return Farey Approximation of 0.7538385', () => { + const approx = fareyApproximation(0.7538385) + expect(approx).toBe({ numerator: 22, denominator: 29 }) + }) + + it('Return Farey Approximation of 0.23584936', () => { + const approx = fareyApproximation(0.23584936) + expet(approx).toBe({ numerator: 13, denominator: 55 }) + }) +}) From 6ac0d61185230650ff225340e63b0f4a58f4ca77 Mon Sep 17 00:00:00 2001 From: Rak Laptudirm Date: Mon, 19 Jul 2021 13:17:06 +0530 Subject: [PATCH 3/5] style: update style --- Maths/test/FareyApproximation.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Maths/test/FareyApproximation.test.js b/Maths/test/FareyApproximation.test.js index bdd07eb64..a9d7bc7bf 100644 --- a/Maths/test/FareyApproximation.test.js +++ b/Maths/test/FareyApproximation.test.js @@ -1,13 +1,13 @@ -import { fareyApproximation } from "../FareyApproximation" +import { fareyApproximation } from '../FareyApproximation' describe('fareyApproximation', () => { it('Return Farey Approximation of 0.7538385', () => { const approx = fareyApproximation(0.7538385) expect(approx).toBe({ numerator: 22, denominator: 29 }) }) - + it('Return Farey Approximation of 0.23584936', () => { const approx = fareyApproximation(0.23584936) - expet(approx).toBe({ numerator: 13, denominator: 55 }) + expect(approx).toBe({ numerator: 13, denominator: 55 }) }) }) From d41b6aade577111436cf0331625b53c782fc2a62 Mon Sep 17 00:00:00 2001 From: Rak Laptudirm Date: Mon, 19 Jul 2021 13:21:35 +0530 Subject: [PATCH 4/5] chore: Update test values --- Maths/test/FareyApproximation.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/test/FareyApproximation.test.js b/Maths/test/FareyApproximation.test.js index a9d7bc7bf..7dce1fc2f 100644 --- a/Maths/test/FareyApproximation.test.js +++ b/Maths/test/FareyApproximation.test.js @@ -3,11 +3,11 @@ import { fareyApproximation } from '../FareyApproximation' describe('fareyApproximation', () => { it('Return Farey Approximation of 0.7538385', () => { const approx = fareyApproximation(0.7538385) - expect(approx).toBe({ numerator: 22, denominator: 29 }) + expect(approx).toBe({ numerator: 52, denominator: 69 }) }) it('Return Farey Approximation of 0.23584936', () => { const approx = fareyApproximation(0.23584936) - expect(approx).toBe({ numerator: 13, denominator: 55 }) + expect(approx).toBe({ numerator: 196, denominator: 831 }) }) }) From e5b7e56f004d682bbeba2cb1a4413ff7c6b0889b Mon Sep 17 00:00:00 2001 From: Rak Laptudirm Date: Mon, 19 Jul 2021 13:25:00 +0530 Subject: [PATCH 5/5] chore: Strict equal for tests --- Maths/test/FareyApproximation.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/test/FareyApproximation.test.js b/Maths/test/FareyApproximation.test.js index 7dce1fc2f..f94024bb9 100644 --- a/Maths/test/FareyApproximation.test.js +++ b/Maths/test/FareyApproximation.test.js @@ -3,11 +3,11 @@ import { fareyApproximation } from '../FareyApproximation' describe('fareyApproximation', () => { it('Return Farey Approximation of 0.7538385', () => { const approx = fareyApproximation(0.7538385) - expect(approx).toBe({ numerator: 52, denominator: 69 }) + expect(approx).toStrictEqual({ numerator: 52, denominator: 69 }) }) it('Return Farey Approximation of 0.23584936', () => { const approx = fareyApproximation(0.23584936) - expect(approx).toBe({ numerator: 196, denominator: 831 }) + expect(approx).toStrictEqual({ numerator: 196, denominator: 831 }) }) })