代码拉取完成,页面将自动刷新
同步操作将从 Gitee 极速下载/javascript-algorithms 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/*
* Let circleRadius is the radius of circle.
* circleRadius is also the side length of the inscribed hexagon
*/
const circleRadius = 1;
/**
* @param {number} sideLength
* @param {number} splitCounter
* @return {number}
*/
function getNGonSideLength(sideLength, splitCounter) {
if (splitCounter <= 0) {
return sideLength;
}
const halfSide = sideLength / 2;
// Liu Hui used the Gou Gu (Pythagorean theorem) theorem repetitively.
const perpendicular = Math.sqrt((circleRadius ** 2) - (halfSide ** 2));
const excessRadius = circleRadius - perpendicular;
const splitSideLength = Math.sqrt((excessRadius ** 2) + (halfSide ** 2));
return getNGonSideLength(splitSideLength, splitCounter - 1);
}
/**
* @param {number} splitCount
* @return {number}
*/
function getNGonSideCount(splitCount) {
// Liu Hui began with an inscribed hexagon (6-gon).
const hexagonSidesCount = 6;
// On every split iteration we make N-gons: 6-gon, 12-gon, 24-gon, 48-gon and so on.
return hexagonSidesCount * (splitCount ? 2 ** splitCount : 1);
}
/**
* Calculate the π value using Liu Hui's π algorithm
*
* @param {number} splitCount - number of times we're going to split 6-gon.
* On each split we will receive 12-gon, 24-gon and so on.
* @return {number}
*/
export default function liuHui(splitCount = 1) {
const nGonSideLength = getNGonSideLength(circleRadius, splitCount - 1);
const nGonSideCount = getNGonSideCount(splitCount - 1);
const nGonPerimeter = nGonSideLength * nGonSideCount;
const approximateCircleArea = (nGonPerimeter / 2) * circleRadius;
// Return approximate value of pi.
return approximateCircleArea / (circleRadius ** 2);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。