+ 2
Digit of Pi with Nilakantha Series / Chudnovsky Algorithm
I'm try to solve Digit of Pi in code coach. Aware of the float limitation I import decimal and set prec to 1000. The idea is to keep calcuating until the result in string has length of 1002 (ignoring the '3.' part). However the code doesn't work as expected and ran into an infinite loop. Then I added a print statement to print the len of the calculated result, it stops at 5x. Am I using the decimal module properly? Or is the algo doesn't fit the playground environment? https://code.sololearn.com/cRTdU7fKxr4j/?ref=app
45 Respostas
+ 3
About the Chudnovsky algorithm.
As far as I understand the number n is the number of approaches, the larger it is, the better the approximation of pi.
Put n more than three and your test will be passed:
pi = chudnovsky(4)
+ 7
No not on sololearn Bob_Li and Solo there are codes in the playground
Look at mpmath
from mpmath import *
https://code.sololearn.com/cTnYZy7uet45/?ref=app
https://code.sololearn.com/cPxfmji3uu3b/?ref=app
+ 6
Ordinarily I would have said yes
__import__("os").system("pip install --upgrade mpmath")
But I noticed mpmath about a year ago Bob_Li
+ 6
My question them falls in the scope of the question as to actual accuracy as in this code raises to mpmath vs math libraries as there is a discrepancy..
* thoughts Solo and Bob_Li
https://code.sololearn.com/cTnYZy7uet45/?ref=app
+ 3
Wong Hei Ming
Nilakantha is too impractical.
I used this
https://code.sololearn.com/c3HwXhmhsIY6/?ref=app
+ 3
BroFar , yes, I have already reported that mpmath can be easily imported and it makes it possible to output the number pi with any size of decimal digits after the decimal point as much as >100,000.
Bob_Li , Wong Hei Ming , by the way, in the "Python Core" course, it is mentioned that Sololearn cannot cover all the libraries since there are a lot of them and suggests that they can additionally be loaded using pip. So in any case, everything is within the framework of the curriculum...😎
+ 3
BroFar , that's right, since "math" represents the number pi in the "float" format.
+ 2
With little research I found the Chudnovsky Algorithm (now added to the title).
The interesting part is it cannot pass all the tests.
It failed when the input is 42, the answer is 9 but my output is 6.
Anyone tried with Chudnovsky Algorithm and pass the tests?
https://code.sololearn.com/c2DEqEVS6eaf/?ref=app
+ 2
Wong Hei Ming
I can barely get by those formulas myself, and I don't even try to remember them. I just bookmark them so I would know where to find them if needed.
I picked this one because it's compact and converges to the value reasonably fast.
I also try to avoid recursive algorithms. If there is an iterative way, it's probably better.
There's whole communities for approximation algorithms online where people collect and compare these codes.
Translating formulas to efficient code is an art that is totally beyond me...
But I guess that with AI, this is going to be trivial..
+ 2
Solo ,
wow, I never guessed that your comment before was from Google translate.
But yes, cranking up n is the way to go.
+ 2
Bob_Li , yes, unfortunately I do not know English and use a translator... 😎
+ 2
Wong Hei Ming, I do not know the tasks, what are the requirements for passing all the tests, I just demonstrated the execution of this code, and you should try to pick up the maximum number of approaches yourself, if it does not work out then we will figure it out further...😎
+ 2
Solo
And unfortunately, I do not know Russian, but the translator works great, and your posts are always helpful.
+ 2
Bob_Li , thanks... 🖐️😎
+ 2
Solo
Because of the hidden cases in code coach we will not know the max digit we need to find.
Now I clear all of them now in python, after more practice with Java I will do it again.
By that time I will try a different approach.
Maybe occasionally try it again with python when I find some formula I am able to code with.
+ 2
Wong Hei Ming
as long as you can generate an accurate 1000 digit pi, getting the correct solution is relatively simple.
I imagine you can just copy paste a 1000+ digit pi string from somewhere
https://newton.ex.ac.uk/research/qsystems/collabs/pi/#:~:text=3.14159265358979323846264338327950288419716939937510%20etc.,it's%20a%20byte%20a%20digit!&text=The%20first%201000000%20decimal%20places,before%20the%20decimal%20point...
and index that so you don't have to do the actual calculation.
+ 2
Bob_Li
While it is true codes often use constant, unfortunately pi is an irrational number, doesn't fall into 'constant'.
If somebody ask what is the 1234th digit of pi, we get stuck again.
At least with your solution we can find the answer quickly.
Find a solution which can handle most of the situation is I'm aiming at.
It is because I'm lazy. Once a solution is found I can just left it behind and do other things, no need to worry to make patches.
+ 2
Bob_Li , you may not believe it, but I also stopped at the Plaff formula and independently wrote code on it that is almost identical to what you have presented here...😎 https://www.sololearn.com/post/1748515/?ref=app
+ 2
Solo
great.
But after knowing mpmath is available in Sololearn, all these fancy methods we are exploring becomes extra work.
This is convenience:
from mpmath import mp
mp.dps = 1002
print(mp.pi)
+ 1
I understand that the Nilakantha series should be up to 1002, if so, then the code should be like this:
# Nilakantha Series
'π=3+4/(2·3·4)-4/(4·5·6)+4/(6·7·8)-4/(8·9·10)+4/(10·11·12)-4/(12·13·14) ⋯'
import decimal
i = 2
pi = ''
formula = '3'
while i < 1002:
af = f'4/({i}*{i+1}*{i+2})'
bf = f'4/({i+2}*{i+3}*{i+4})'
formula = formula + '+' + af + '-' + bf
tmp = decimal.Decimal(eval(formula))
pi = str(tmp)
i += 4
print(formula)
print(pi)
print(len(pi))