Skip to content
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

Add Liu Hui's π algorithm #49

Closed
mantou132 opened this issue Jun 4, 2018 · 3 comments
Closed

Add Liu Hui's π algorithm #49

mantou132 opened this issue Jun 4, 2018 · 3 comments
Labels
enhancement New feature or request

Comments

@mantou132
Copy link
Contributor

mantou132 commented Jun 4, 2018

https://en.wikipedia.org/wiki/Liu_Hui's_%CF%80_algorithm

e.g:

const getSideLength = (sideLength, count) => {
  if (count <= 0) return sideLength;
  const m = sideLength / 2;
  const g = Math.sqrt((0.5 ** 2) - (m ** 2));
  const j = 0.5 - g;
  return getSideLength(Math.sqrt((j ** 2) + (m ** 2)), count - 1);
};


const pi = (splitCount = 0) => {
  const sideLength = getSideLength(0.5, splitCount);
  const sideCount = 6 * (splitCount ? 2 ** splitCount : 1);
  return sideLength * sideCount;
};

// for (let i = 0; i < 100; i += 1) {
//   const p = pi(i);
//   console.log(i, p, p === Math.PI);
// }

export default pi;
@trekhleb trekhleb added the enhancement New feature or request label Jun 5, 2018
@trekhleb
Copy link
Owner

trekhleb commented Jun 6, 2018

Hi @mantou132 !
Thank you for suggestion! Would you please create a pull request for this algorithm?

@mantou132
Copy link
Contributor Author

Submitted PR #61

@trekhleb
Copy link
Owner

Thank you @mantou132
The code is already in master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants