Liu Hui `π` Algorithm
Definition
- Definition
- Explanation
- Guidance
- Tips
Liu Hui's π
Algorithm is a ancient Chinese method devised by mathematician Liu Hui in the 3rd century CE to approximate the value of π
. It employs the idea of inscribing and circumscribing polygons around a circle to iteratively approach the value of π
The algorithm approximates the value of π
by inscribing and circumscribing regular polygons around a circle. It uses the properties of polygons and trigonometric functions to calculate perimeters and approximate π
- Start with a circle of radius 1
- Begin with a regular polygon inscribed within the circle. The more sides it has, the closer the approximation
- Compute the perimeter of the inscribed polygon
- Construct a regular polygon circumscribing the circle
- Find the perimeter of the circumscribed polygon
- repeat steps with polygons of increasing sides
- Use the formula to approximate:
π ≈ (Perimeter of circumscribed polygon + Perimeter of inscribed polygon) / 2
- start with a low number of sides for the polygons and gradually increase it for better precision
- ensure accuracy in calculating perimeters to avoid cumulative errors
- use trigonometric functions to calculate side lengths and angles of polygons accurately
Practice
- Practice
- Solution
liuHuiPiApproximation():
radius = 1
inscribed_perimeter = 0
circumscribed_perimeter = 0
for sides in range(3, maximum_sides):
inscribed_perimeter = calculatePolygonPerimeter(radius, sides, True)
circumscribed_perimeter = calculatePolygonPerimeter(radius, sides, False)
pi_approximation = (inscribed_perimeter + circumscribed_perimeter) / 2
print("Approximation of π with", sides, "sides:", pi_approximation)
calculatePolygonPerimeter(radius, sides, inscribed):
if inscribed:
polygon_side_length = 2 * radius * sin(π / sides)
else:
polygon_side_length = 2 * radius * tan(π / sides)
return sides * polygon_side_length
package main
import (
"math"
)
func liuHui(n int) float64 {
sideLength := 2.0
polygonSides := 6.0
for i := 0; i < n; i++ {
sideLength = math.Sqrt(4 - math.Pow(sideLength/2, 2))
polygonSides *= 2
}
return sideLength * polygonSides
}
public class LiuHuiAlgorithm {
public static double liuHui(int n) {
double sideLength = 2.0;
double polygonSides = 6.0;
for (int i = 0; i < n; i++) {
sideLength = Math.sqrt(4 - Math.pow(sideLength / 2, 2));
polygonSides *= 2;
}
return sideLength * polygonSides;
}
}
function liuHui(n) {
let sideLength = 2.0;
let polygonSides = 6.0;
for (let i = 0; i < n; i++) {
sideLength = Math.sqrt(4 - Math.pow(sideLength / 2, 2));
polygonSides *= 2;
}
return sideLength * polygonSides;
}
fun liuHui(n: Int): Double {
var sideLength = 2.0
var polygonSides = 6.0
repeat(n) {
sideLength = Math.sqrt(4 - Math.pow(sideLength / 2, 2))
polygonSides *= 2
}
return sideLength * polygonSides
}
import math
def liu_hui(n):
side_length = 2.0
polygon_sides = 6.0
for _ in range(n):
side_length = math.sqrt(4 - (side_length / 2) ** 2)
polygon_sides *= 2
return side_length * polygon_sides
fn liu_hui(n: u32) -> f64 {
let mut side_length = 2.0;
let mut polygon_sides = 6.0;
for _ in 0..n {
side_length = (4.0 - (side_length / 2.0).powi(2)).sqrt();
polygon_sides *= 2.0;
}
side_length * polygon_sides
}
function liuHui(n: number): number {
let sideLength: number = 2.0;
let polygonSides: number = 6.0;
for (let i = 0; i < n; i++) {
sideLength = Math.sqrt(4 - Math.pow(sideLength / 2, 2));
polygonSides *= 2;
}
return sideLength * polygonSides;
}