Skip to content

feat: Add Liu Hui's π algorithm #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.idea
coverage
.vscode
9 changes: 9 additions & 0 deletions src/algorithms/graph/liu-hui-pi/README.md
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions src/algorithms/graph/liu-hui-pi/__test__/liu-hui-pi.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
53 changes: 53 additions & 0 deletions src/algorithms/graph/liu-hui-pi/liu-hui-pi.js
Original file line number Diff line number Diff line change
@@ -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;