From 7dc9b80f62a89197aedcd8a9afa46fb5062498d2 Mon Sep 17 00:00:00 2001 From: mantou <709922234@qq.com> Date: Tue, 12 Jun 2018 19:54:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Add=20Liu=20Hui's=20=CF=80=20algorithm?= =?UTF-8?q?=20(#61)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + src/algorithms/graph/liu-hui-pi/README.md | 9 ++++ .../liu-hui-pi/__test__/liu-hui-pi.test.js | 16 ++++++ src/algorithms/graph/liu-hui-pi/liu-hui-pi.js | 53 +++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 src/algorithms/graph/liu-hui-pi/README.md create mode 100644 src/algorithms/graph/liu-hui-pi/__test__/liu-hui-pi.test.js create mode 100644 src/algorithms/graph/liu-hui-pi/liu-hui-pi.js diff --git a/.gitignore b/.gitignore index c7f4bdc0..640c0608 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules .idea coverage +.vscode diff --git a/src/algorithms/graph/liu-hui-pi/README.md b/src/algorithms/graph/liu-hui-pi/README.md new file mode 100644 index 00000000..b95790b6 --- /dev/null +++ b/src/algorithms/graph/liu-hui-pi/README.md @@ -0,0 +1,9 @@ +# Liu Hui's π Algorithm + +Multiply one side of a hexagon by the radius (of its circumcircle), then multiply this by three, to yield the area of a dodecagon; if we cut a hexagon into a dodecagon, multiply its side by its radius, then again multiply by six, we get the area of a 24-gon; the finer we cut, the smaller the loss with respect to the area of circle, thus with further cut after cut, the area of the resulting polygon will coincide and become one with the circle; there will be no loss + + + +## References + +- [Liu Hui's π Algorithm on Wikipedia](https://en.wikipedia.org/wiki/Liu_Hui's_%CF%80_algorithm) diff --git a/src/algorithms/graph/liu-hui-pi/__test__/liu-hui-pi.test.js b/src/algorithms/graph/liu-hui-pi/__test__/liu-hui-pi.test.js new file mode 100644 index 00000000..3f22f762 --- /dev/null +++ b/src/algorithms/graph/liu-hui-pi/__test__/liu-hui-pi.test.js @@ -0,0 +1,16 @@ +import pi from '../liu-hui-pi'; + +describe('Liu Hui\'s π algorithm', () => { + it('Dodecagon π', () => { + expect(pi(1)).toBe(3); + }); + it('24-gon π', () => { + expect(pi(2)).toBe(3.105828541230249); + }); + it('6144-gon π', () => { + expect(pi(10)).toBe(3.1415921059992717); + }); + it('201326592-gon π', () => { + expect(pi(25)).toBe(3.141592653589793); + }); +}); diff --git a/src/algorithms/graph/liu-hui-pi/liu-hui-pi.js b/src/algorithms/graph/liu-hui-pi/liu-hui-pi.js new file mode 100644 index 00000000..7174a488 --- /dev/null +++ b/src/algorithms/graph/liu-hui-pi/liu-hui-pi.js @@ -0,0 +1,53 @@ +// Liu Hui began with an inscribed hexagon. +// Let r is the radius of circle. +// r is also the side length of the inscribed hexagon +const c = 6; +const r = 0.5; + +const getSideLength = (sideLength, count) => { + if (count <= 0) return sideLength; + const m = sideLength / 2; + + // Liu Hui used the Gou Gu theorem repetitively. + const g = Math.sqrt((r ** 2) - (m ** 2)); + const j = r - g; + return getSideLength(Math.sqrt((j ** 2) + (m ** 2)), count - 1); +}; + +const getSideCount = splitCount => c * (splitCount ? 2 ** splitCount : 1); + +/** + * Calculate the π value using Liu Hui's π algorithm + * + * Liu Hui argued: + * Multiply one side of a hexagon by the radius (of its circumcircle), + * then multiply this by three, to yield the area of a dodecagon; if we + * cut a hexagon into a dodecagon, multiply its side by its radius, then + * again multiply by six, we get the area of a 24-gon; the finer we cut, + * the smaller the loss with respect to the area of circle, thus with + * further cut after cut, the area of the resulting polygon will coincide + * and become one with the circle; there will be no loss + * @param {Number} splitCount repeat times + * @return {Number} + */ +const pi = (splitCount = 1) => { + const sideLength = getSideLength(r, splitCount - 1); + const sideCount = getSideCount(splitCount - 1); + const p = sideLength * sideCount; + const area = (p / 2) * r; + return area / (r ** 2); +}; + +// !test +// for (let i = 1; i < 26; i += 1) { +// const p = pi(i); +// console.log( +// 'split count: %f, side count: %f, π: %f, is Math.PI? %o', +// i, +// getSideCount(i), +// p, +// p === Math.PI, +// ); +// } + +export default pi;