Skip to content

Bbp formula #1989

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

Closed
wants to merge 12 commits into from
Closed

Bbp formula #1989

wants to merge 12 commits into from

Conversation

ken437
Copy link
Contributor

@ken437 ken437 commented May 16, 2020

Describe your change:

Adds an algorithm that approximates pi using the Bailey-Borwein-Plouffe formula, described at https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@ken437
Copy link
Contributor Author

ken437 commented May 16, 2020

I didn't intend to include aliquot_sum.py in the pull request but it was added for some reason. Is there a way to make my pull request only include the addition of bbp_formula.py?

@TheSuperNoob
Copy link
Contributor

TheSuperNoob commented May 16, 2020

This is pretty cool! One issue with this implementation however:

Currently it is limited to Python’s standard double precision calculation. Which means regardless of how many iterations you supply you function with it will not get any more precise than that. To get around this we can use Python’s builtin module Decimal which allows for arbitrary precision.

One note about the formula itself:

This is mainly used for finding the n-th number of pi without needing to calculate any of the previous digits! Which means we can efficiently calculate the 1 billionth digit of pi without needing to know the digits leading up to that one. So maybe another PR can be opened using with this same file but implements a new function called nth_digit_of_pi() or something similar to more fully explore the power of this formula.

Comment on lines 32 to 33
# num_iterations + 1 because sum notation includes the last term
for index in range(num_iterations + 1):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the + 1 part actually is needed. By definition this is an infinite sum which just gets more and more accurate with each iteration, so anytime you stop is technically the last iteration :)
And it might fit better to how you define num_iterations as well. As an example, default value for num_iterations is 1000, and starting with 0 and ending with 999 gives you a total of 1000 iterations.

Very minor detail though and shouldn't affect the final result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to design it so that num_iterations represented the number on top of the sigma expression in the formula. Now that I think about it, though, it might have been better to name the variable something like upper_limit_of_summation rather than num_iterations

@cclauss
Copy link
Member

cclauss commented May 17, 2020

I didn't intend to include aliquot_sum.py

Rebase this branch on master.

@ken437
Copy link
Contributor Author

ken437 commented May 17, 2020

This is pretty cool! One issue with this implementation however:

Currently it is limited to Python’s standard double precision calculation. Which means regardless of how many iterations you supply you function with it will not get any more precise than that. To get around this we can use Python’s builtin module Decimal which allows for arbitrary precision.

One note about the formula itself:

This is mainly used for finding the n-th number of pi without needing to calculate any of the previous digits! Which means we can efficiently calculate the 1 billionth digit of pi without needing to know the digits leading up to that one. So maybe another PR can be opened using with this same file but implements a new function called nth_digit_of_pi() or something similar to more fully explore the power of this formula.

The first paragraph definitely sounds like a good idea. Maybe I could even let the user input the number of digits after the decimal point to round to (e.g. if they input "2" they get 3.14). As for the second paragraph, I could definitely do that in another pull request; I think it would be cool if our repository had an efficient general pi algorithm in one file and a digit extraction algorithm in another file.

@TheSuperNoob
Copy link
Contributor

The first paragraph definitely sounds like a good idea. Maybe I could even let the user input the number of digits after the decimal point to round to (e.g. if they input "2" they get 3.14).

Yes you can do this! From what i can tell each iteration of this formula gives 1 digit of pi so theres not a whole lot you need to change, only update the calculations to be of type Decimal where the precision is equal to the number of iterations you do. :)

I think it would be cool if our repository had an efficient general pi algorithm in one file and a digit extraction algorithm in another file.

I have already in the past implemented Chudnovsky's algorithm which is as far as im aware the most efficiant method of calculating many digits of pi. chudnovsky_algorithm.py
this yields around 14 digits of pi each iteration.

@cclauss cclauss added the tests are failing Do not merge until tests pass label May 18, 2020
@TheSuperNoob
Copy link
Contributor

I assume this revision is abandoned in favor of #1996?

@stale
Copy link

stale bot commented Jun 21, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Used to mark an issue or pull request stale. label Jun 21, 2020
@stale
Copy link

stale bot commented Jun 28, 2020

Please reopen this issue once you commit the changes requested or make improvements on the code. Thank you for your contributions.

@stale stale bot closed this Jun 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Used to mark an issue or pull request stale. tests are failing Do not merge until tests pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants