Skip to content

Commit 1e2fdc6

Browse files
committed
Refactor README.
1 parent 7dc9b80 commit 1e2fdc6

File tree

5 files changed

+118
-43
lines changed

5 files changed

+118
-43
lines changed

src/algorithms/graph/liu-hui-pi/README.md

-9
This file was deleted.

src/algorithms/graph/liu-hui-pi/__test__/liu-hui-pi.test.js

-16
This file was deleted.

src/algorithms/math/liu-hui/README.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Liu Hui's π Algorithm
2+
3+
Liu Hui remarked in his commentary to The Nine Chapters on the Mathematical Art,
4+
that the ratio of the circumference of an inscribed hexagon to the diameter of
5+
the circle was `three`, hence `π` must be greater than three. He went on to provide
6+
a detailed step-by-step description of an iterative algorithm to calculate `π` to
7+
any required accuracy based on bisecting polygons; he calculated `π` to
8+
between `3.141024` and `3.142708` with a 96-gon; he suggested that `3.14` was
9+
a good enough approximation, and expressed `π` as `157/50`; he admitted that
10+
this number was a bit small. Later he invented an ingenious quick method to
11+
improve on it, and obtained `π ≈ 3.1416` with only a 96-gon, with an accuracy
12+
comparable to that from a 1536-gon. His most important contribution in this
13+
area was his simple iterative `π` algorithm.
14+
15+
## Area of a circle
16+
17+
Liu Hui argued:
18+
19+
> Multiply one side of a hexagon by the radius (of its
20+
circumcircle), then multiply this by three, to yield the
21+
area of a dodecagon; if we cut a hexagon into a
22+
dodecagon, multiply its side by its radius, then again
23+
multiply by six, we get the area of a 24-gon; the finer
24+
we cut, the smaller the loss with respect to the area
25+
of circle, thus with further cut after cut, the area of
26+
the resulting polygon will coincide and become one with
27+
the circle; there will be no loss
28+
29+
![Liu Hui](https://upload.wikimedia.org/wikipedia/commons/6/69/Cutcircle2.svg)
30+
31+
Liu Hui's method of calculating the area of a circle.
32+
33+
Further, Liu Hui proved that the area of a circle is half of its circumference
34+
multiplied by its radius. He said:
35+
36+
> Between a polygon and a circle, there is excess radius. Multiply the excess
37+
radius by a side of the polygon. The resulting area exceeds the boundary of
38+
the circle
39+
40+
In the diagram `d = excess radius`. Multiplying `d` by one side results in
41+
oblong `ABCD` which exceeds the boundary of the circle. If a side of the polygon
42+
is small (i.e. there is a very large number of sides), then the excess radius
43+
will be small, hence excess area will be small.
44+
45+
> Multiply the side of a polygon by its radius, and the area doubles;
46+
hence multiply half the circumference by the radius to yield the area of circle.
47+
48+
![Liu Hui](https://upload.wikimedia.org/wikipedia/commons/9/95/Cutcircle.svg)
49+
50+
The area within a circle is equal to the radius multiplied by half the
51+
circumference, or `A = r x C/2 = r x r x π`.
52+
53+
## Iterative algorithm
54+
55+
Liu Hui began with an inscribed hexagon. Let `M` be the length of one side `AB` of
56+
hexagon, `r` is the radius of circle.
57+
58+
![Liu Hui](https://upload.wikimedia.org/wikipedia/commons/4/46/Liuhui_geyuanshu.svg)
59+
60+
Bisect `AB` with line `OPC`, `AC` becomes one side of dodecagon (12-gon), let
61+
its length be `m`. Let the length of `PC` be `j` and the length of `OP` be `G`.
62+
63+
`AOP`, `APC` are two right angle triangles. Liu Hui used
64+
the [Gou Gu](https://en.wikipedia.org/wiki/Pythagorean_theorem) (Pythagorean theorem)
65+
theorem repetitively:
66+
67+
![](https://wikimedia.org/api/rest_v1/media/math/render/svg/dbfc192c78539c3901c7bad470302ededb76f813)
68+
69+
![](https://wikimedia.org/api/rest_v1/media/math/render/svg/ccd12a402367c2d6614c88e75006d50bfc3a9929)
70+
71+
![](https://wikimedia.org/api/rest_v1/media/math/render/svg/65d77869fc02c302d2d46d45f75ad7e79ae524fb)
72+
73+
![](https://wikimedia.org/api/rest_v1/media/math/render/svg/a7a0d0d7f505a0f434e5dd80c2fef6d2b30d6100)
74+
75+
![](https://wikimedia.org/api/rest_v1/media/math/render/svg/c31b9acf38f9d1a248d4023c3bf286bd03007f37)
76+
77+
![](https://wikimedia.org/api/rest_v1/media/math/render/svg/0dee798efb0b1e3e64d6b3542106cb3ecaa4a383)
78+
79+
![](https://wikimedia.org/api/rest_v1/media/math/render/svg/3ffeafe88d2983b364ad3442746063e3207fe842)
80+
81+
82+
From here, there is now a technique to determine `m` from `M`, which gives the
83+
side length for a polygon with twice the number of edges. Starting with a
84+
hexagon, Liu Hui could determine the side length of a dodecagon using this
85+
formula. Then continue repetitively to determine the side length of a
86+
24-gon given the side length of a dodecagon. He could do this recursively as
87+
many times as necessary. Knowing how to determine the area of these polygons,
88+
Liu Hui could then approximate `π`.
89+
90+
## References
91+
92+
- [Wikipedia](https://en.wikipedia.org/wiki/Liu_Hui%27s_%CF%80_algorithm)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import liuHui from '../liuHui';
2+
3+
describe('liHui', () => {
4+
it('Dodecagon π', () => {
5+
expect(liuHui(1)).toBe(3);
6+
});
7+
8+
it('24-gon π', () => {
9+
expect(liuHui(2)).toBe(3.105828541230249);
10+
});
11+
12+
it('6144-gon π', () => {
13+
expect(liuHui(10)).toBe(3.1415921059992717);
14+
});
15+
16+
it('201326592-gon π', () => {
17+
expect(liuHui(25)).toBe(3.141592653589793);
18+
});
19+
});

src/algorithms/graph/liu-hui-pi/liu-hui-pi.js src/algorithms/math/liu-hui/liuHui.js

+7-18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const getSideLength = (sideLength, count) => {
1111
// Liu Hui used the Gou Gu theorem repetitively.
1212
const g = Math.sqrt((r ** 2) - (m ** 2));
1313
const j = r - g;
14+
1415
return getSideLength(Math.sqrt((j ** 2) + (m ** 2)), count - 1);
1516
};
1617

@@ -27,27 +28,15 @@ const getSideCount = splitCount => c * (splitCount ? 2 ** splitCount : 1);
2728
* the smaller the loss with respect to the area of circle, thus with
2829
* further cut after cut, the area of the resulting polygon will coincide
2930
* and become one with the circle; there will be no loss
30-
* @param {Number} splitCount repeat times
31-
* @return {Number}
31+
*
32+
* @param {number} splitCount repeat times
33+
* @return {number}
3234
*/
33-
const pi = (splitCount = 1) => {
35+
export default function liuHui(splitCount = 1) {
3436
const sideLength = getSideLength(r, splitCount - 1);
3537
const sideCount = getSideCount(splitCount - 1);
3638
const p = sideLength * sideCount;
3739
const area = (p / 2) * r;
38-
return area / (r ** 2);
39-
};
40-
41-
// !test
42-
// for (let i = 1; i < 26; i += 1) {
43-
// const p = pi(i);
44-
// console.log(
45-
// 'split count: %f, side count: %f, π: %f, is Math.PI? %o',
46-
// i,
47-
// getSideCount(i),
48-
// p,
49-
// p === Math.PI,
50-
// );
51-
// }
5240

53-
export default pi;
41+
return area / (r ** 2);
42+
}

0 commit comments

Comments
 (0)